下载文件①

下载文件需要将byte数组还原成文件。

首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式)。然后使用OutputStream将文件输入

  1. @RequestMapping(value = "downPhotoById")
  2. public void downPhotoByStudentId(String id, final HttpServletResponse response){
  3. PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);
  4. byte[] data = entity.getPhotoData();
  5. String fileName = entity.getFileName()== null ? "照片.png" : entity.getFileName();
  6. fileName = URLEncoder.encode(fileName, "UTF-8");
  7. response.reset();
  8. response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  9. response.addHeader("Content-Length", "" + data.length);
  10. response.setContentType("application/octet-stream;charset=UTF-8");
  11. OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
  12. outputStream.write(data);
  13. outputStream.flush();
  14. outputStream.close();
  15. }
  1. <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

 下载文件④

  1. @RequestMapping("/export")
  2. public ResponseEntity<byte[]> export() throws IOException {
  3. HttpHeaders headers = new HttpHeaders();
  4. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  5. headers.setContentDispositionFormData("attachment", "dict.txt");
  6. return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File("C:/Users/Administrator/Desktop/a.txt")),
  7. headers, HttpStatus.CREATED);
  8. }
 
 

Spring MVC实现文件下载的更多相关文章

  1. Spring MVC 的文件下载

    在看Spring MVC文件下载之前请先看Spring MVC文件上传 地址:http://www.cnblogs.com/dj-blog/p/7535101.html 文件下载比较简单,在超链接中指 ...

  2. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  3. Spring MVC学习纲要

    感慨一下 之前用过Spring MVC, MyBatis,但是很久不用之后发现很多知识点都荒废了,毕竟工作就是重复,重复再重复.没有啥新东西.所以还是找个时间把忘了的东西捡起来.万一搞了个大bug,然 ...

  4. Spring MVC文件下载

    方案一: // 文件下载 @RequestMapping(value = "/downloadFile") public ResponseEntity<byte[]> ...

  5. Spring MVC 文件上传 & 文件下载

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...

  6. spring mvc的excel报表文件下载时流的冲突解决

    在jsp或者在servlet中有时要用到 response.getOutputStream(),但是此时会在后台报这个错误java.lang.IllegalStateException: getOut ...

  7. spring mvc 文件下载 get请求解决中文乱码问题

    方案简写,自己或有些基础的可以看懂,因为没时间写的那么详细 方案1 spring mvc解决get请求中文乱码问题, 在tamcat中server.xml文件 URIEncoding="UT ...

  8. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  9. Spring MVC 学习总结(三)——请求处理方法Action详解

    Spring MVC中每个控制器中可以定义多个请求处理方法,我们把这种请求处理方法简称为Action,每个请求处理方法可以有多个不同的参数,以及一个多种类型的返回结果. 一.Action参数类型 如果 ...

随机推荐

  1. IOS组件绑定无效错误

    报错的原因:界面按钮事件没有绑定到源代码或者相关的代码被注释了.比如你的button组件以及绑定到IBOutlet,但是viewcontrol.m上没有相关的代码,就会出现异常.

  2. virtualbox 中ubantu虚拟机范文win7文件夹

    宿主机操作系统: windows7_x64 旗舰版 Oracle VM virtualBox版本:5.0.12 虚拟机操作系统: Ubantu15.12_x64 Desktop版本   让虚拟机可以访 ...

  3. centos查看硬件信息

    服务器硬件信息常见, 是经常要操作的事: 下面分享一些常见的硬件查看命令: 1.centos 下查看硬件信息内容非常全面. CentOS常用命令查看cpu more /proc/cpuinfo 2.C ...

  4. JavaScript Patterns 3.8 Error Objects

    The error objects created by constructors(Error(),  SyntaxError(), TypeError(), and others) have the ...

  5. jacob 实现Office Word文件格式转换

    关于jacob用法,百度一下就会发现几乎都是复制2004年一个代码,那段代码实现的是从一个目录读取所有doc文件,然后把它转html格式. 为了便习学习和使用,我把代码看懂后精简了一下,得出不少新结论 ...

  6. 利用API方式进行数据库的增删改查

    /* 将数据库的增删改查单独放进一个包 */ package com.itheima28.sqlitedemo.dao; import java.util.ArrayList; import java ...

  7. html不使用cache数据

    <HEAD>      <META   HTTP-EQUIV="Pragma"   CONTENT="no-cache">     &l ...

  8. sql server 2005 32位+64位、企业版+标准版、CD+DVD 下载地址大全 .

    企业版DVD SQL Server 2005 Enterprise Edition(支持超大型企业) 32 位DVD: ed2k://|file|cs_sql_2005_ent_x86_dvd.iso ...

  9. 移动web在ios和android下点击元素出现阴影问题

     移动web开发经验总结 1.-webkit-tap-highlight-color:rgba(255,255,255,0)可以同时屏蔽ios和android下点击元素时出现的阴影.备注:transp ...

  10. jmap,jhat分析内存

    分析JAVA Application的内存使用时,jmap是一个很实用的轻量级工具.使用jmap可以查看heap空间的概要情况,粗略的掌握heap的使用情况.也可以生成heapdump文件,再使用jh ...