From cdf2aa0d36c5eab18cb15b0490cddce574017d66 Mon Sep 17 00:00:00 2001
From: Jinquan_Ou <Jinquan@gdut.com>
Date: 星期日, 19 三月 2023 17:47:26 +0800
Subject: [PATCH] 修改下载

---
 zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java |   55 +++++++++++++++++++++++++++
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java |   10 +++++
 zhang-content/src/main/java/com/ruoyi/service/DownLoadFileService.java          |   14 +++++++
 3 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
index 6569b5d..1cad5e4 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java
@@ -6,6 +6,7 @@
 import javax.servlet.http.HttpServletResponse;
 import javax.websocket.server.PathParam;
 
+import com.ruoyi.service.DownLoadFileService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -34,6 +35,9 @@
 
     @Autowired
     private ServerConfig serverConfig;
+
+    @Autowired
+    private DownLoadFileService downLoadFileService;
 
     private static final String FILE_DELIMETER = ",";
 
@@ -69,6 +73,12 @@
         }
     }
 
+    @GetMapping("/downLoadFile")
+    public AjaxResult downLoadFile(@RequestParam("path") String path,HttpServletResponse response) throws Exception {
+        return downLoadFileService.downLoadFile(path,response);
+    }
+
+
     /**
      * 閫氱敤涓婁紶璇锋眰锛堝崟涓級
      */
diff --git a/zhang-content/src/main/java/com/ruoyi/service/DownLoadFileService.java b/zhang-content/src/main/java/com/ruoyi/service/DownLoadFileService.java
new file mode 100644
index 0000000..dbdb470
--- /dev/null
+++ b/zhang-content/src/main/java/com/ruoyi/service/DownLoadFileService.java
@@ -0,0 +1,14 @@
+package com.ruoyi.service;
+
+import com.ruoyi.common.core.domain.AjaxResult;
+
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @Version 1.0
+ * @Author Jin_quan Ou
+ * @Date 2023-03-19 17:32
+ */
+public interface DownLoadFileService {
+    AjaxResult downLoadFile(String path, HttpServletResponse response);
+}
diff --git a/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java b/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java
new file mode 100644
index 0000000..96a331b
--- /dev/null
+++ b/zhang-content/src/main/java/com/ruoyi/service/impl/DownLoadFileServiceImpl.java
@@ -0,0 +1,55 @@
+package com.ruoyi.service.impl;
+
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.service.DownLoadFileService;
+import org.springframework.stereotype.Service;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+
+/**
+ * @Version 1.0
+ * @Author Jin_quan Ou
+ * @Date 2023-03-19 17:33
+ */
+@Service
+public class DownLoadFileServiceImpl implements DownLoadFileService {
+    @Override
+    public AjaxResult downLoadFile(String path, HttpServletResponse response) {
+
+        File file = new File(path);
+        byte[] buffer = new byte[1024];
+        BufferedInputStream bis = null;
+        OutputStream os = null;
+        try {
+            //鏂囦欢鏄惁瀛樺湪
+            if (file.exists()) {
+                //璁剧疆鍝嶅簲
+                response.setContentType("application/octet-stream;charset=UTF-8");
+                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
+                response.setHeader("Content-Disposition","attachment;filename=");
+                response.setCharacterEncoding("UTF-8");
+                os = response.getOutputStream();
+                bis = new BufferedInputStream(new FileInputStream(file));
+                while(bis.read(buffer) != -1){
+                    os.write(buffer);
+                }
+            }
+        }catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if(bis != null) {
+                    bis.close();
+                }
+                if(os != null) {
+                    os.flush();
+                    os.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return AjaxResult.success("鎿嶄綔鎴愬姛");
+    }
+}

--
Gitblit v1.9.1