Browse Source

文件下载调整

hujie 1 year ago
parent
commit
f556f2ad1b

+ 18 - 3
cn.com.taiji.system/src/main/java/cn/com/taiji/zhongxiao/web/FileDown.java

@@ -9,6 +9,8 @@ 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;
@@ -19,12 +21,20 @@ import org.springframework.web.bind.annotation.RequestMapping;
 @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 = ResourceUtils.getFile("classpath:templates/download-excel-template/" + name + ".xlsx");
+				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"));
@@ -71,8 +81,13 @@ public class FileDown {
 			FileInputStream ins = null;
 			OutputStream out = null ;
 			try {
-				File file = ResourceUtils.getFile("classpath:templates/download-excel-template/" + name + "." + type);
-
+				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));