下载文件①

下载文件需要将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. 怎样在VS2010中打开VS2012的项目

    VS2012中对C#的支持度非常好,不管是编写方便程度(不需要插件就能高亮代码及代码自动提示功能),还对MFC的一些功能优化很多. 我们可以修改两个工程文件来把VS2012的工程文件一直到VS2010 ...

  2. H5文件操作API

    引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨浏览器.跨设备等情况下实现统一的表现,从另外一个 ...

  3. Java 图片处理——如何生成高清晰度而占有磁盘小的缩略图

    现在的web项目,图片越来越多,图片大小也越来越大,随便就能达到1M,2M,甚至更大.用户上传的图片,一般是无法直接使用的.一般要生成两三种对应的缩略图,分别适配不同的终端,不同的场景.比如PC,手机 ...

  4. python 解析json loads dumps

    认识 引用模块 重要函数 案例 排序 缩进参数 压缩 参考 认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standa ...

  5. APP 接口开发及读取静态缓存

    <?php /** * Description: App 接口 * Create date:2015-10-19 13:36 * Author: zhaoyingnan **/ class Re ...

  6. Hive drop table batched

    if the hive version not support drop table tablename purge. your drop table command will move data t ...

  7. Linux shell basic2 cat find tr

    Cat stands for concatenate. Case 1. When the text files have more blank lines, we want to remove the ...

  8. JVM的内存区域划分

            JVM的内存区域划分 学过C语言的朋友都知道C编译器在划分内存区域的时候经常将管理的区域划分为数据段和代码段,数据段包括堆.栈以及静态数据区.那么在Java语言当中,内存又是如何划分的 ...

  9. Hadoop中WritableComparable 和 comparator

    1.WritableComparable 查看HadoopAPI,如图所示: WritableComparable继承自Writable和java.lang.Comparable接口,是一个Writa ...

  10. hdu1047 Integer Inquiry

    /* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...