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参数类型 如果 ...
随机推荐
- 3、IOS开发--iPad之仿制QQ空间 (为HomeViewController添加交互逻辑 并 为导航条内容添加UISegmentedControl)
1. 为bottomMenu添加点击效果 思路描述: 需求: 点击BottomButton的三个item,然后对应响应的是HomeViewController弹出对应的业务 ...
- iOS之UI--关于modal
modal的效果展示: 关于modal的涉及到的知识点: 1.当一个控制器被销毁的时候,它里面所有子控制器的业务逻辑都不能够处理 2.当一个控制器被销毁的时候,它里面所有子控件的业务逻辑都不能够处理 ...
- Oracle SQL Developer连接报错(ORA-12505)
Oracle SQL Developer连接报错(ORA-12505) 之前我的Oracle数据库出现问题,费大波周折终于弄好了,今天又创建了一个DBA管理员的连接方式出现问题,本人现在把解决方案分享 ...
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
原文链接地址:http://www.cppblog.com/Tim/archive/2012/07/04/181018.html 本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们 ...
- Windows环境下maven 安装与环境变量配置
Maven是一个项目管理的Java 工具,在JavaEE中,我们可以使用Maven方便地管理团队合作的项目,现在我们在学习JavaEE框架,使用Maven可以管理类库,有效方便地供团队中的其他人员使用 ...
- 第一次wubi安装Ubuntu的经历及所走的弯路
#安装目标:利用xp存储剩余空间安装ubuntu, 形成双系统. 整理出待安装的磁盘空间 #需要无损磁盘工具, 用了"傲梅分区助手", 偷懒没有选其他高大上的英文软件. XP下硬盘 ...
- Legends-ggplot2图例的一些操作
移除图例 require(ggplot2) b = qplot(Sepal.Length,Petal.Length,data=iris,geom="point",colour = ...
- Python HeapSort
__author__ = 'student' print 'hello world hello python' ''' heap sort root leftchild 2n+1 rightchild ...
- 【读书笔记《Android游戏编程之从零开始》】12.游戏开发基础(Canvas 画布)
1.Canvas 画布 画布类 Canvas 封装了图形和图片绘制等内容,此类常用的函数说明如下: drawColor(int color) 作用:绘制颜色覆盖画布,常用于刷屏 参数:颜色值,也可用十 ...
- Debian下安装deb格式安装包
dpkg -i 软件包名称 就好啦 下面是相应链接: http://blog.csdn.net/lhf_tiger/article/details/7493400