下载文件①

下载文件需要将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. android 之 animations 动画

    android 提供的了两种机制你可以用来创建简单的动画:tweedned animation(渐变动画) 和 frame-by-frame animation(逐帧动画)(有道翻译的,汗!!!) . ...

  2. jhljx跑跑跑(找规律)

    题目来源:https://biancheng.love/contest/41/problem/D/index jhljx跑跑跑 题目描述 数学不好的jhljx又在和别人打牌,他们一共m人每人n张牌,牌 ...

  3. UVa 100 - The 3n + 1 problem(函数循环长度)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  4. DEV 财务货币格式单元格

    在用友金蝶等财务软件中,经常需要输入货币类型的数据, 那么这种输入框要如何制作呢? 扩展DataGridView 的功能  出自在天空飞翔博客 http://www.cnblogs.com/micha ...

  5. WebService核心之WSDL深入详解

    WebService核心之WSDL深入详解 根据上一篇文章开发的Web Service实例生成的WSDL文档如下: XML里两个属性介绍: targetNamespace          相当于ja ...

  6. ARP协议的报文格式

    原文链接地址:http://www.cnblogs.com/laojie4321/archive/2012/04/12/2444187.html   结构ether_header定义了以太网帧首部:结 ...

  7. GridControl控件的数据显示的样式控制(转)

    如上两图所示,Dev列表控件GridControl默认的格式并没有渐变变色效果,显示的日期数据,也是“yyyy-MM-dd”的格式,而非“yyyy-MM-dd HH:mm:ss”即使对于后面有长格式的 ...

  8. windows上用netstat查看端口/进程占用

    windows上用netstat命令查看某个端口是否占用,被哪个进程所占用 1.查看端口的占用情况,获取进程的PID 命令: netstat -ano | findstr "<端口号& ...

  9. Example to use django queryset

    from django.db.models import get_app, get_models, get_model from django.db import models #get the ce ...

  10. 18 多线程编程 - 《Python 核心编程》