最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

分享Spring的下載組件

 更新時(shí)間:2016年05月29日 10:12:17   作者:learn_more  
這篇文章主要為大家分享了Spring的下載組件,感興趣的小伙伴們可以參考一下

本文為大家分享了Spring4的下載組件,供大家參考,具體內(nèi)容如下

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 文件對(duì)象
   * @return 文件字節(jié)流
   */
  public ResponseEntity<byte[]> export(File file) {
    return export(file.getName(), file);
  }
 
 
  /**
   * 下載文件通用方法
   *
   * @param fileName 文件名稱
   * @param file   文件對(duì)象
   * @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 是因?yàn)?IE Edge 無(wú)法識(shí)別,但是Firefox chrome 沒(méi)問(wèn)題
      rs = new ResponseEntity<>(FileUtils.readFileToByteArray(file),
          headers, HttpStatus.OK);
    } catch (IOException e) {
      //throw new CommonException(ResponseStatusEnum.FILE_ERROR, e);
    }
    return rs;
  }
 
  /**
   * 下載文件的名稱,這個(gè)是在瀏覽器那里顯示的名稱
   *
   * @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");
      }
      // 把加號(hào)還原為空格(IE Edge 有問(wèn)題)
      name = name.replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
      //throw new CommonException(ResponseStatusEnum.FAILURE, e);
    }
    return name;
  }
 
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

得荣县| 贵阳市| 靖边县| 景泰县| 中江县| 松桃| 张家港市| 玉门市| 安宁市| 千阳县| 大同县| 博湖县| 武隆县| 百色市| 石渠县| 富宁县| 唐河县| 尉犁县| 山阴县| 上林县| 深水埗区| 大邑县| 大渡口区| 娄底市| 皋兰县| 赞皇县| 福州市| 夹江县| 耿马| 东乌珠穆沁旗| 玉田县| 广东省| 瓦房店市| 中卫市| 拉萨市| 荆州市| 长沙县| 鹤峰县| 临澧县| 台北县| 故城县|