Spring MVC实现文件下载
方法一:
@RequestMapping("/testHttpMessageDown")public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {File file = new File(request.getSession().getServletContext().getClassLoader().getResource("test.xlsx").getPath());byte[] body = null;InputStream is = new FileInputStream(file);body = new byte[is.available()];is.read(body);HttpHeaders headers = new HttpHeaders();headers.add("Content-Disposition", "attchement;filename=" + file.getName());HttpStatus statusCode = HttpStatus.OK;ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);return entity;}
方法二:
/** Download a file from * - inside project, located in resources folder.* - outside project, located in File system somewhere. */@RequestMapping(value="/download", method = RequestMethod.GET)public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {File file = new File(request.getSession().getServletContext().getClassLoader().getResource("test.xlsx").getPath());if(!file.exists()){String errorMessage = "Sorry. The file you are looking for does not exist";System.out.println(errorMessage);OutputStream outputStream = response.getOutputStream();outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));outputStream.close();return;}String mimeType= URLConnection.guessContentTypeFromName(file.getName());if(mimeType==null){System.out.println("mimetype is not detectable, will take default");mimeType = "application/octet-stream";}System.out.println("mimetype : "+mimeType);response.setContentType(mimeType);/* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() +"\""));/* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*///response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); response.setContentLength((int)file.length());InputStream inputStream = new BufferedInputStream(new FileInputStream(file));//Copy bytes from source to destination(outputstream in this example), closes both streams. FileCopyUtils.copy(inputStream, response.getOutputStream());}
Maven示例:
https://github.com/easonjim/5_java_example/tree/master/springmvc/test1
参考:
http://www.yiibai.com/spring_mvc/spring-mvc-4-file-download-example.html
http://blog.csdn.net/wuzuodingfeng/article/details/53489089
==>如有问题,请联系我:easonjim#163.com,或者下方发表评论。<==
Spring MVC实现文件下载相关推荐
- Spring MVC 文件上传 文件下载
索引: 目录索引 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadController.java Files_Ut ...
- zbb20180613 Spring MVC实现大文件下载功能
Spring MVC实现大文件下载功能 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
- Spring MVC环境中的文件上传功能实现
在实际开发过程中,尤其是web项目开发,文件上传和下载的需求的功能非常场景,比如说用户头像.商品图片.邮件附件等等.其实文件上传下载的本质都是通过流的形式进行读写操作,而在开发中不同的框架都会对文件上 ...
- Spring系列之一 Spring MVC
摘要: 最近在看Spring的书,之前一直跟着项目做,虽然项目用到了Spring的很多功能,但是我很少有机会在工作的项目中去配置Spring.感觉只有理论是不够的,虽然只是用Spring的配置让人感觉 ...
- Spring MVC中的视图解析ViewResolver
http://blog.csdn.net/prince2270/article/details/5891085 在Spring MVC中,当Controller将请求处理结果放入到ModelAndVi ...
- Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解
转载自https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的 ...
- java spring mvc 上传_Java Spring MVC 上传下载文件配置及controller方法详解
下载: 1.在spring-mvc中配置(用于100M以下的文件下载) 下载文件代码 @RequestMapping("/file/{name.rp}") public Respo ...
- spring mvc 实现单文件 || 多文件上传
文件上传 1. pom依赖(jar包) 2. 文件上传解析器配置 3. 上传实现 4. 下载||文件展示实现(io流的实现) 项目下载地址 https://github.com/sevenyounga ...
- Http请求中Content-Type讲解以及在Spring MVC中的应用
引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...
最新文章
- zend studio 8安装与汉化
- [NC16591]关押罪犯 并查集
- 大数据 清华 覃征_调剂到清华读研?不想去!清华大学大数据调剂生放弃录取!...
- 连接sql sever2008数据库出现了无法连接到数据库引擎问题解决
- OpenCV 机器视觉入门精选 100 题(附 Python 代码)
- java kmp算法_KMP算法java版实现
- Direct3D播放RGB(通过Texture)
- 2020-python小工能
- java ordered list_Java - JSON对象和ListOrderedMap问题
- Docker集群的创建与管理
- Cinema 4D* 中令人惊叹的体积效果
- DataFactory插入MySQL中文乱码问题
- 深入Linux文件系统
- 【全网最实用】最常用Windows快捷键和Windows命令整理
- 计算机常见故障判断方法,电脑故障判断-计算机常见故障判断方法
- 概率dp(A - Scout YYF I POJ - 3744 )
- vue:如何设计网页的微信扫码登录功能
- 程序包解析错误解决办法
- 虚幻引擎UE4背包系统(如何制作可拖动(Drag and Drop)的背包(Scrollbox))
- Golang big.int类型转int