소스 검색

专业库模板下载

hexiao 2 년 전
부모
커밋
e991f32a57

+ 4 - 8
purchase-admin/src/main/java/com/ozs/web/controller/base/BaseProfessionalController.java

@@ -117,15 +117,11 @@ public class BaseProfessionalController extends BaseController {
     public void downloadZip(HttpServletRequest request, HttpServletResponse response) {
         try {
             // 文件路径
-            String downloadPath = this.getClass().getResource("/template/professional.xlsx").getPath();
-//            InputStream resourceAsStream = this.getClass().getResourceAsStream("/template/专业库模板.xlsx");
-            log.info("downloadPath:{}",downloadPath);
-            // 下载文件
-            String realFileName = "专业库模板.xlsx";
-            String filePath = downloadPath;
+//            String downloadPath = this.getClass().getResource("/template/professional.xlsx").getPath();
+            InputStream resourceAsStream = this.getClass().getResourceAsStream("/template/professional.xlsx");
             response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
-            FileUtils.setAttachmentResponseHeader(response, realFileName);
-            FileUtils.writeBytes(filePath, response.getOutputStream());
+            FileUtils.setAttachmentResponseHeader(response, "专业库模板.xlsx");
+            FileUtils.writeBytesByInput(resourceAsStream, response.getOutputStream());
 
         } catch (Exception e) {
             log.error("下载文件失败" , e);

+ 18 - 7
purchase-common/src/main/java/com/ozs/common/utils/file/FileUtils.java

@@ -1,12 +1,6 @@
 package com.ozs.common.utils.file;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.net.URLEncoder;
 import java.net.UnknownHostException;
 import java.nio.charset.StandardCharsets;
@@ -360,4 +354,21 @@ public class FileUtils {
     }
 
 
+    public static void writeBytesByInput(InputStream ins, OutputStream os) throws IOException {
+        try {
+            if (ObjectUtils.isEmpty(ins)) {
+                throw new FileNotFoundException();
+            }
+            byte[] b = new byte[1024];
+            int length;
+            while ((length = ins.read(b)) > 0) {
+                os.write(b, 0, length);
+            }
+        } catch (IOException e) {
+            throw e;
+        } finally {
+            IOUtils.close(os);
+            IOUtils.close(ins);
+        }
+    }
 }