当前位置: 移动技术网 > IT编程>开发语言>Java > 分享Spring的下载组件

分享Spring的下载组件

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文为大家分享了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 文件字节流
   */
  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;
  }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网