使用springmvc中ResponseEntity下载文件
来源:互联网 发布:微信招聘制作软件 编辑:程序博客网 时间:2024/06/05 16:35
今天遇见了一个点击table列表中文件名,实现下载文件的功能。
因为这边的项目用的springmvc做的容器,以下是通过ajax访问该url通过输入流将数据(该数据通过url携带)中携带的文件内容(content)转换成字节存入缓存中。
通过springmvc封装的下载实现ResponseEntity将字符串输出成.docx文件下载。
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
通过设置请求体(body),请求头(headers),请求状态(statusCode)传回前端页面。
import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import java.io.ByteArrayInputStream;import java.io.IOException;@RequestMapping("/download/{content}/{title}")public ResponseEntity<byte[]> download(HttpServletRequest request,@PathVariable String content ,@PathVariable String title ) throws IOException { //设置缓存 byte[] body = null; //字节流读取String数据 ByteArrayInputStream is = new ByteArrayInputStream("content".getBytes()); body = new byte[is.available()]; is.read(body); //设置请求头 HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attchement;filename=" + title+".docx"); //设置请求状态 HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode); return entity;}
页面不能通过ajax访问url。
必须通过页面window.location.href = resqURL;属性来访问。
暂时遇见url中文乱码问题,待解决。
阅读全文
0 0
- 使用springmvc中ResponseEntity下载文件
- SpringMVC使用ResponseEntity实现文件下载
- springmvc ResponseEntity 下载文件损坏问题解决方法
- springmvc ResponseEntity 下载文件损坏问题解决方法
- SpringMVC之使用ResponseEntity
- ResponseEntity实现文件下载
- 利用 spring mvc ResponseEntity 做文件下载
- 利用 spring mvc ResponseEntity 做文件下载
- 使用springmvc下载文件
- java鬼混笔记:用Spring的ResponseEntity进行文件下载
- 文件下载-SpringMVC中测试
- SpringMvc中静态文件下载。
- 使用springmvc实现文件下载
- SpringMVC中controller中方法返回类型为ResponseEntity<String>乱码的问题
- 使用 SpringMVC 实现文件下载功能
- 使用springMVC实现文件上传和下载之文件下载
- 使用Itext做pdf文档下载--返回前端类型为ResponseEntity(byte[])
- SpringMVC—文件下载
- mysql查询今天、昨天、7天、近30天、本月、上一月 数据
- 通过源码详解 Servlet
- Java虚拟机专题之垃圾回收(读书笔记)
- Android中WindowManager.LayoutParams类详解
- 如何用命令将本地项目上传到git
- 使用springmvc中ResponseEntity下载文件
- 正则表达式
- VAX破解版支持VS2010和2013,代码智能提示,安装就能用,不用替换文件
- Android使用iconfont图库
- 2017.12.11.9
- 操作系统—存储器管理
- 软件的作用
- FaceBook原生广告接入——最白话,手把手教你做系列。
- 20171219Link