123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package cn.com.taiji.zhongxiao.web;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.io.FileUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Controller;
- import org.springframework.util.ResourceUtils;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- @RequestMapping(value="/fileDown")
- public class FileDown {
- @Value("${spring.profiles.active}")
- private String profile;
- @RequestMapping("/download{name}")
- public void fileDownload(@PathVariable("name") String name ,HttpServletRequest request, HttpServletResponse response){ //Model models,
- FileInputStream ins = null;
- OutputStream out = null ;
- try {
- File file = File.createTempFile(name, ".xlsx");
- file.deleteOnExit();
- if ("prod".equals(profile)) {
- FileUtils.copyInputStreamToFile(this.getClass().getClassLoader().getResourceAsStream("templates/download-excel-template/" + name + ".xlsx"), file);
- } else {
- file = ResourceUtils.getFile("classpath:templates/download-excel-template/" + name + ".xlsx");
- }
- //设置响应头,控制浏览器下载该文件
- if(file.exists()){
- response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name+".xlsx"));
- response.setContentType("application/vnd.ms-excel;charset=utf-8");
- response.setCharacterEncoding("UTF-8");
- //读取要下载的文件,保存到文件输入流
- ins = new FileInputStream(file);
- //创建输出流
- out = response.getOutputStream();
- //创建缓冲区
- byte buffer[] = new byte[1024];
- int len = 0;
- //循环将输入流中的内容读取到缓冲区当中
- while((len=ins.read(buffer))>0){
- //输出缓冲区的内容到浏览器,实现文件下载
- out.write(buffer, 0, len);
-
- }
-
- }else{
- response.setHeader("Warning" ,"199 Miscellaneous warning");
- response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("文件不存在或者失效.html", "UTF-8"));
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }finally{
- try {
- //关闭文件输入流
- if (ins != null) {
- ins.close();
- }
- //关闭输出流
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
- @RequestMapping("/alldownload{name}-{type}")
- public void allfileDownload(@PathVariable("name") String name ,@PathVariable("type") String type, HttpServletRequest request, HttpServletResponse response){ //Model models,
- FileInputStream ins = null;
- OutputStream out = null ;
- try {
- File file = File.createTempFile(name, "." + type);
- file.deleteOnExit();
- if ("prod".equals(profile)) {
- FileUtils.copyInputStreamToFile(this.getClass().getClassLoader().getResourceAsStream("templates/download-excel-template/" + name + "." + type), file);
- } else {
- file = ResourceUtils.getFile("classpath:templates/download-excel-template/" + name + "." + type);
- }
- //设置响应头,控制浏览器下载该文件
- if(file.exists()){
- response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name+"."+type));
- response.setContentType("application/octet-stream;charset=utf-8");
- response.setCharacterEncoding("UTF-8");
- //读取要下载的文件,保存到文件输入流
- ins = new FileInputStream(file);
- //创建输出流
- out = response.getOutputStream();
- //创建缓冲区
- byte buffer[] = new byte[1024];
- int len = 0;
- //循环将输入流中的内容读取到缓冲区当中
- while((len=ins.read(buffer))>0){
- //输出缓冲区的内容到浏览器,实现文件下载
- out.write(buffer, 0, len);
-
- }
-
- }else{
- response.setHeader("Warning" ,"199 Miscellaneous warning");
- response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("文件不存在或者失效.html", "UTF-8"));
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }finally{
- try {
- //关闭文件输入流
- if (ins != null) {
- ins.close();
- }
- //关闭输出流
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
-
- }
|