发布于 2016-07-06 11:17:56 | 122 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


这篇文章主要为大家分享了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 文件字节流
   */
  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;
  }
 
 
}

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务