Spring MVC实现文件下载
下载文件①
下载文件需要将byte数组还原成文件。
首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式)。然后使用OutputStream将文件输入
- @RequestMapping(value = "downPhotoById")
- public void downPhotoByStudentId(String id, final HttpServletResponse response){
- PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);
- byte[] data = entity.getPhotoData();
- String fileName = entity.getFileName()== null ? "照片.png" : entity.getFileName();
- fileName = URLEncoder.encode(fileName, "UTF-8");
- response.reset();
- response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
- response.addHeader("Content-Length", "" + data.length);
- response.setContentType("application/octet-stream;charset=UTF-8");
- OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
- outputStream.write(data);
- outputStream.flush();
- outputStream.close();
- }
- <a href="<%=request.getContextPath() %>/downPhotoById.do?id=8000001">下载照片</a>
下载文件②
/** * @Description 下载文件
* @author jxldjsn
* @date 2015年12月11日 下午6:11:33
* @param fileName
* @param file
* @return
* @throws IOException
*/
public ResponseEntity<byte[]> download(String fileName, File file) throws IOException {
String dfileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", dfileName);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }
下载文件③
//文件下载 主要方法
public static void download(HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType
) throws Exception { request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null; //获取项目根目录
String ctxPath = request.getSession().getServletContext()
.getRealPath(""); //获取下载文件露肩
String downLoadPath = ctxPath+"/uploadFile/"+ storeName; //获取文件的长度
long fileLength = new File(downLoadPath).length(); //设置文件输出类型
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(storeName.getBytes("utf-8"), "ISO8859-1"));
//设置输出长度
response.setHeader("Content-Length", String.valueOf(fileLength));
//获取输入流
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
//输出流
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
//关闭流
bis.close();
bos.close();
} } 下载直接访问控制器如:http:\\localhost:8080/springmvc/download.do
下载文件④
- @RequestMapping("/export")
- public ResponseEntity<byte[]> export() throws IOException {
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- headers.setContentDispositionFormData("attachment", "dict.txt");
- return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("C:/Users/Administrator/Desktop/a.txt")),
- headers, HttpStatus.CREATED);
- }
Spring MVC实现文件下载的更多相关文章
- Spring MVC 的文件下载
在看Spring MVC文件下载之前请先看Spring MVC文件上传 地址:http://www.cnblogs.com/dj-blog/p/7535101.html 文件下载比较简单,在超链接中指 ...
- Spring MVC的文件上传和下载
简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...
- Spring MVC学习纲要
感慨一下 之前用过Spring MVC, MyBatis,但是很久不用之后发现很多知识点都荒废了,毕竟工作就是重复,重复再重复.没有啥新东西.所以还是找个时间把忘了的东西捡起来.万一搞了个大bug,然 ...
- Spring MVC文件下载
方案一: // 文件下载 @RequestMapping(value = "/downloadFile") public ResponseEntity<byte[]> ...
- Spring MVC 文件上传 & 文件下载
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...
- spring mvc的excel报表文件下载时流的冲突解决
在jsp或者在servlet中有时要用到 response.getOutputStream(),但是此时会在后台报这个错误java.lang.IllegalStateException: getOut ...
- spring mvc 文件下载 get请求解决中文乱码问题
方案简写,自己或有些基础的可以看懂,因为没时间写的那么详细 方案1 spring mvc解决get请求中文乱码问题, 在tamcat中server.xml文件 URIEncoding="UT ...
- Http请求中Content-Type讲解以及在Spring MVC中的应用
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
- Spring MVC 学习总结(三)——请求处理方法Action详解
Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果. 一.Action参数类型 如果 ...
随机推荐
- IOS之KVC和KVO(未完待续)
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 介绍一种css水平垂直居中的方法(非常好用!)
这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...
- 为什么需要SQL Profile
为什么需要SQL Profile Why oracle need SQL Profiles,how it work and what are SQL Profiles... 使用DBMS_XPLAN. ...
- 查看mysql主从配置的状态及修正 slave不启动问题
1.查看master的状态 mysql> show master status; //Position不应该为0 mysql> show processlist; //state状态应 ...
- Swing应用开发实战系列之二:设计日期选择面板窗口
Swing本身没有提供什么华丽丽的日期时间选择控件,所以笔者就在网上搜了个第三方的jar包jdatepicker-1.3.2.jar,基于此设计了个很轻量的日期选择面板,很简单的.效果图如下所示: 代 ...
- nyoj 237 游戏高手的烦恼 二分匹配--最小点覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237 二分匹配--最小点覆盖模板题 Tips:用邻接矩阵超时,用数组模拟邻接表WA,暂时只 ...
- 最小的k个数
// 最小的k个数.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include & ...
- Hadoop could not find or load main class
Error: Could not find or load main class <class_name> 我在尝试使用hadoop definitive guide的代码做练习时,遇到一 ...
- java poi read write xlsx
package myjava; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExce ...
- SQL-一道特殊的字符串分解题目
本题不是一道直接的字符串拆解, 应用场景如下,表中有一个字段,是表示事件受影响的国家集合,使用逗号进行分隔,不幸的是,居然发现有些国家本身就带有逗号,这样在规范化的时候,如何准确地找到这些国家呢? 以 ...