分享Spring的下载组件

编程之家收集整理的这篇文章主要介绍了分享Spring的下载组件编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这篇文章主要为大家分享了Spring的下载组件,感兴趣的小伙伴们可以参考一下

本文为大家分享了Spring4的下载组件,供大家参考,具体内容如下

package com.hnust.common.controller; import org.apache.commons.io.FileUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RestController; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /** * Created by Heweipo on 2016/5/27. * * 下载通用控制器 */ @RestController public class DownloadController extends BaseController { /** * 下载文件通用方法 * * @param file 文件对象 * @return 文件字节流 */ public ResponseEntity export(File file) { return export(file.getName(), file); } /** * 下载文件通用方法 * * @param fileName 文件名称 * @param file 文件对象 * @return 文件字节流 */ public ResponseEntity export(String fileName, File file) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentdispositionFormData("attachment", encodeFileName(fileName)); ResponseEntity rs = null; try { // 这里不能使用 HttpStatus.CREATED 201 是因为 IE Edge 无法识别,但是Firefox chrome 没问题 rs = new ResponseEntity(FileUtils.readFiletoByteArray(file), headers, HttpStatus.OK); } catch (IOException e) { //throw new CommonException(ResponseStatusEnum.FILE_ERROR, e); } return rs; } /** * 下载文件名称,这个是在浏览器那里显示名称 * * @param fileName 文件名称 * @return 加码的文件名称 *

* IE * Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko *

* Edge * Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 *

* Firefox * Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0 *

* Chrome * Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 */ private String encodeFileName(String fileName) { String name = fileName; try { String agent = request.getHeader("USER-AGENT").toLowerCase(); if (null != agent && (agent.contains("msie") || agent.contains("edge"))) { // IE edge name = URLEncoder.encode(fileName, "UTF-8"); } else if (agent.contains("safari") || agent.contains("chrome") || agent.contains("firefox")) { // safari chrome firefox name = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } else { // IE10 IE11 name = URLEncoder.encode(fileName, "UTF-8"); } // 把加号还原为空格(IE Edge 有问题) name = name.replace("+", "%20"); } catch (UnsupportedEncodingException e) { //throw new CommonException(ResponseStatusEnum.FAILURE, e); } return name; } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


今天小编给大家分享的是Java读取resources中资源文件路径以及jar中文件无法读取如何解决,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容...
本篇文章和大家了解一下javaweb中servlet技术的用法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。ServletServlet(Server Applet)是Jav...
今天小编给大家分享的是JavaScrip简单数据类型隐式转换如何实现,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会...
这篇文章主要介绍了只返回实体类中的部分字段问题如何解决,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。如何只返回实体类中的部分字段在...
今天小编给大家分享的是java的预定义Class对象的方法,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的...
今天小编给大家分享的是Java中线程安全的实现思路介绍,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的...
今天小编给大家分享的是Java中如何实现String字符串分割,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获...
《》是一本介绍Java图形用户界面编程的实践指南。该书由Java编程专家编写,旨在帮助读者深入了解Java GUI编程的基本原理和实践技巧,从而能够开发出高质量、易用性强的GUI应用程序。