FileDown.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package cn.com.taiji.zhongxiao.web;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.net.URLEncoder;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.commons.io.FileUtils;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.util.ResourceUtils;
  13. import org.springframework.web.bind.annotation.PathVariable;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. @Controller
  16. @RequestMapping(value="/fileDown")
  17. public class FileDown {
  18. @Value("${spring.profiles.active}")
  19. private String profile;
  20. @RequestMapping("/download{name}")
  21. public void fileDownload(@PathVariable("name") String name ,HttpServletRequest request, HttpServletResponse response){ //Model models,
  22. FileInputStream ins = null;
  23. OutputStream out = null ;
  24. try {
  25. File file = File.createTempFile(name, ".xlsx");
  26. file.deleteOnExit();
  27. if ("prod".equals(profile)) {
  28. FileUtils.copyInputStreamToFile(this.getClass().getClassLoader().getResourceAsStream("templates/download-excel-template/" + name + ".xlsx"), file);
  29. } else {
  30. file = ResourceUtils.getFile("classpath:templates/download-excel-template/" + name + ".xlsx");
  31. }
  32. //设置响应头,控制浏览器下载该文件
  33. if(file.exists()){
  34. response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name+".xlsx"));
  35. response.setContentType("application/vnd.ms-excel;charset=utf-8");
  36. response.setCharacterEncoding("UTF-8");
  37. //读取要下载的文件,保存到文件输入流
  38. ins = new FileInputStream(file);
  39. //创建输出流
  40. out = response.getOutputStream();
  41. //创建缓冲区
  42. byte buffer[] = new byte[1024];
  43. int len = 0;
  44. //循环将输入流中的内容读取到缓冲区当中
  45. while((len=ins.read(buffer))>0){
  46. //输出缓冲区的内容到浏览器,实现文件下载
  47. out.write(buffer, 0, len);
  48. }
  49. }else{
  50. response.setHeader("Warning" ,"199 Miscellaneous warning");
  51. response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("文件不存在或者失效.html", "UTF-8"));
  52. }
  53. } catch (IOException e1) {
  54. e1.printStackTrace();
  55. }finally{
  56. try {
  57. //关闭文件输入流
  58. if (ins != null) {
  59. ins.close();
  60. }
  61. //关闭输出流
  62. if (out != null) {
  63. out.close();
  64. }
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. @RequestMapping("/alldownload{name}-{type}")
  71. public void allfileDownload(@PathVariable("name") String name ,@PathVariable("type") String type, HttpServletRequest request, HttpServletResponse response){ //Model models,
  72. FileInputStream ins = null;
  73. OutputStream out = null ;
  74. try {
  75. File file = File.createTempFile(name, "." + type);
  76. file.deleteOnExit();
  77. if ("prod".equals(profile)) {
  78. FileUtils.copyInputStreamToFile(this.getClass().getClassLoader().getResourceAsStream("templates/download-excel-template/" + name + "." + type), file);
  79. } else {
  80. file = ResourceUtils.getFile("classpath:templates/download-excel-template/" + name + "." + type);
  81. }
  82. //设置响应头,控制浏览器下载该文件
  83. if(file.exists()){
  84. response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name+"."+type));
  85. response.setContentType("application/octet-stream;charset=utf-8");
  86. response.setCharacterEncoding("UTF-8");
  87. //读取要下载的文件,保存到文件输入流
  88. ins = new FileInputStream(file);
  89. //创建输出流
  90. out = response.getOutputStream();
  91. //创建缓冲区
  92. byte buffer[] = new byte[1024];
  93. int len = 0;
  94. //循环将输入流中的内容读取到缓冲区当中
  95. while((len=ins.read(buffer))>0){
  96. //输出缓冲区的内容到浏览器,实现文件下载
  97. out.write(buffer, 0, len);
  98. }
  99. }else{
  100. response.setHeader("Warning" ,"199 Miscellaneous warning");
  101. response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("文件不存在或者失效.html", "UTF-8"));
  102. }
  103. } catch (IOException e1) {
  104. e1.printStackTrace();
  105. }finally{
  106. try {
  107. //关闭文件输入流
  108. if (ins != null) {
  109. ins.close();
  110. }
  111. //关闭输出流
  112. if (out != null) {
  113. out.close();
  114. }
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120. }