分享Spring的下載組件
更新時間:2016年05月29日 10:12:17 作者:learn_more
這篇文章主要為大家分享了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. * <p> * 下載通用控制器 */ @RestController public class DownloadController extends BaseController { /** * 下載文件通用方法 * * @param file 文件對象 * @return 文件字節(jié)流 */ public ResponseEntity<byte[]> export(File file) { return export(file.getName(), file); } /** * 下載文件通用方法 * * @param fileName 文件名稱 * @param file 文件對象 * @return 文件字節(jié)流 */ public ResponseEntity<byte[]> export(String fileName, File file) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", encodeFileName(fileName)); ResponseEntity<byte[]> 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 加碼的文件名稱 * <p> * IE * Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko * <p> * 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 * <p> * Firefox * Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0 * <p> * 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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 基于Spring Mvc實現(xiàn)的Excel文件上傳下載示例
- 在SpringMVC框架下實現(xiàn)文件的上傳和下載示例
- Spring MVC 文件上傳下載的實例
- SpringMvc導出Excel實例代碼
- 學習SpringMVC——國際化+上傳+下載詳解
- Java Spring MVC 上傳下載文件配置及controller方法詳解
- MyBatis與SpringMVC相結合實現(xiàn)文件上傳、下載功能
- springMVC配置環(huán)境實現(xiàn)文件上傳和下載
- 基于spring+hibernate+JQuery開發(fā)之電子相冊(附源碼下載)
- SpringMVC實現(xiàn)文件下載功能
相關文章
詳解SpringBoot AOP 攔截器(Aspect注解方式)
這篇文章主要介紹了詳解SpringBoot AOP 攔截器 Aspect,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05J2EE Servlet基礎在瀏覽器上運行HelloServlet的方法
這篇文章主要介紹了J2EE Servlet基礎在瀏覽器上運行HelloServlet的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10關于Springboot+gateway整合依賴并處理依賴沖突問題
這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問題,給大家提到了spring boot版本和spring cloud版本,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01Java代碼規(guī)范與質量檢測插件SonarLint的使用
本文主要介紹了Java代碼規(guī)范與質量檢測插件SonarLint的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08