首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

从服务器上载文件到客户端

2012-11-09 
从服务器下载文件到客户端页面连接:后台代码://下载文件public void download(){String filePath Str

从服务器下载文件到客户端
页面连接:



后台代码:
//下载文件public void download(){        String filePath = "";        String fileName = "";try {// 读取文件并且设置相关参数if (StringUtils.isNotBlank(url)) {filePath = request.getSession().getServletContext().getRealPath(url);}if (StringUtils.isNotBlank(name)) {fileName = new String(name.getBytes("gbk"),"iso8859-1");}File file = new File(filePath);byte[] buf = new byte[1*1024];int len = 0;BufferedInputStream br = null;OutputStream ut = null;response.reset();// 必须加,不然保存不了临时文件response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment; filename="+ fileName);br = new BufferedInputStream(new FileInputStream(file));ut = response.getOutputStream();while ((len = br.read(buf)) != -1) {ut.write(buf, 0, len);}} catch (Exception e) {e.printStackTrace();}}

热点排行