国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

分享Spring的下載組件

2019-11-26 14:16:54
字體:
來源:轉載
供稿:網友

本文為大家分享了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> * 下載通用控制器 */@RestControllerpublic class DownloadController extends BaseController {   /**   * 下載文件通用方法   *   * @param file 文件對象   * @return 文件字節流   */  public ResponseEntity<byte[]> export(File file) {    return export(file.getName(), file);  }    /**   * 下載文件通用方法   *   * @param fileName 文件名稱   * @param file   文件對象   * @return 文件字節流   */  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;  }  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天祝| 凤翔县| 嘉义县| 临潭县| 敦化市| 平武县| 东辽县| 始兴县| 南江县| 广元市| 五莲县| 柘城县| 神池县| 班戈县| 乐山市| 翁源县| 古蔺县| 临安市| 宁武县| 仙居县| 肇庆市| 吴川市| 西城区| 白山市| 巴彦县| 上饶县| 丰台区| 南皮县| 南投市| 江安县| 蕉岭县| 保靖县| 承德县| 桑日县| 赤城县| 阳新县| 理塘县| 怀集县| 土默特左旗| 和静县| 永春县|