java 文件下载
2016-06-14 16:56
519 查看
文件下载,无非是通过流读取文件然后写出文件
注意:这种方式下载只能通过window.location.href=”download/downloadFile.do”的方式,不能通过ajax来调用
@RequestMapping("/download") @ResponseBody //filename 为上传存的文件名称,例如:201601011111_2345.png public void downloadFile(String filename, HttpServletResponse response,HttpServletRequest request) throws IOException { String destpath = ""; destpath = request.getRealPath("/upload/images") + File.separator; filepath = destpath + filename; File file = new File(filepath); response.setContentType("multipart/form-data;charset=utf-8"); //防止乱码 response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO8859-1")); response.setCharacterEncoding("utf-8"); InputStream fis = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[fis.available()]; fis.read(b); response.getOutputStream().write(b); response.getOutputStream().flush(); response.getOutputStream().close(); response.flushBuffer(); fis.close(); }
注意:这种方式下载只能通过window.location.href=”download/downloadFile.do”的方式,不能通过ajax来调用
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树