yusm 6 ヶ月 前
コミット
f099dc6354

+ 32 - 4
fhKeeper/formulahousekeeper/management-workshop/src/main/java/com/management/platform/controller/CommonUploadController.java

@@ -5,14 +5,18 @@ import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
+import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.UUID;
 
 @RestController
@@ -26,6 +30,9 @@ public class CommonUploadController {
     @Value(value = "${upload.tempUpdatePath}")
     private String tempUpdatePath;
 
+    @Value(value = "${logging.path}")
+    private String logPath;
+
     @RequestMapping(value="uploadFile")
     public HttpRespMsg uploadFile(MultipartFile multipartFile) {
         HttpRespMsg msg = new HttpRespMsg();
@@ -97,4 +104,25 @@ public class CommonUploadController {
 
         return msg;
     }
+
+    @RequestMapping("/downLoadLog")
+    public ResponseEntity<byte[]> downLoadLog() throws IOException {
+        // 🧐🧐🧐读取本地的文件
+        // 获取File对象
+        System.out.println("读取目录=="+logPath);
+        logger.info("读取目录=={}"+logPath);
+        String fileName = "workshop.out";
+        File readFile=new File(logPath+fileName);
+        // 🐳🐳🐳设置响应头,把文件名称放入响应头中,确保文件可下载
+        HttpHeaders headers = new HttpHeaders();
+        headers.set("Content-Disposition", "attachment;filename=" + URLEncoder.encode(readFile.getName(), "UTF-8"));
+        // 🐳🐳🐳设置内容类型为「application/octet-stream」二进制流
+        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
+
+        Path path = Paths.get(readFile.toURI());
+        // 获取File对象的字节码文件
+        byte[] bytes = Files.readAllBytes(path);
+        //return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
+        return ResponseEntity.ok().headers(headers).body(bytes);
+    }
 }