浏览代码

八爪鱼下载日志文件修改

yurk 2 年之前
父节点
当前提交
f313e073ea

+ 2 - 22
fhKeeper/formulahousekeeper/octopus/src/views/Home.vue

@@ -15,7 +15,8 @@
                 </div>
             </el-col>
             <el-col :span="10" class="userinfo">
-                <el-button type="text" style="margin-right:30px; color:#fff" @click="downLoadLog" >下载工时管家日志</el-button>
+                <!-- <el-button type="text" style="margin-right:30px; color:#fff" @click="downLoadLog" >下载工时管家日志</el-button> -->
+                <a style="margin-right:30px; text-decoration:none; color:#fff" href="/api/common/downLoadLog">下载工时管家日志</a>
                 <el-dropdown trigger="hover" style="margin-right:30px;">
                     <span class="el-dropdown-link userinfo-inner">
                         <i class="el-icon-user" style="font-size:18px" ></i>
@@ -365,27 +366,6 @@
                 //     });
                 // });
             },
-            //下载日志文件
-            downLoadLog() {
-                this.http.post('/common/downLoadLog',{},
-                res => {
-                    if (res.code == "ok") {
-                        var filePath = res.data;
-                        const a = document.createElement('a'); // 创建a标签
-                        a.setAttribute('download',"wt_print.log"+ '.log');// download属性
-                        a.setAttribute('href', filePath);// href链接
-                        a.click(); //自执行点击事件
-                        a.remove();
-                    } 
-                },
-                error => {
-                    this.$message({
-                        message: error,
-                        type: "error"
-                    });
-                    }
-                );
-            },
 
             //点击消息的跳转
             locationHerf(id, date, type) {

+ 29 - 22
fhKeeper/formulahousekeeper/ops-platform/src/main/java/com/management/platform/controller/CommonUploadController.java

@@ -12,14 +12,26 @@ import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.FileCopyUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.math.BigDecimal;
+import java.net.URLEncoder;
 import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.UUID;
@@ -31,6 +43,8 @@ public class CommonUploadController {
     Logger logger = LogManager.getLogger(org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME);
     @Value(value = "${upload.path}")
     private String path;
+    @Value(value = "${logDownLoad.path}")
+    private String logDownLoadPath;
 
     @RequestMapping(value="uploadFile")
     public HttpRespMsg uploadFile(MultipartFile multipartFile) {
@@ -68,27 +82,20 @@ public class CommonUploadController {
         return msg;
     }
     @RequestMapping("/downLoadLog")
-    public HttpRespMsg downLoadLog() throws IOException {
-        HttpRespMsg httpRespMsg=new HttpRespMsg();
-        FileChannel sourceChannel = null;
-        FileChannel destChannel = null;
-        try {
-            File file=new File("/www/worktime/server/");
-            if(!file.exists()){
-                file.mkdirs();
-            }
-            sourceChannel = new FileInputStream("/www/worktime/server/wt_print.log").getChannel();
-            destChannel = new FileOutputStream(path+"wt_print.log").getChannel();
-            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
-        } finally{
-            sourceChannel.close();
-            destChannel.close();
-        }
-        httpRespMsg.data="/upload/"+"wt_print.log";
-        return httpRespMsg;
+    public ResponseEntity<byte[]> downLoadLog() throws IOException {
+        // 🧐🧐🧐读取本地的文件
+        // 获取File对象
+        File readFile=new File(logDownLoadPath);
+        // 🐳🐳🐳设置响应头,把文件名称放入响应头中,确保文件可下载
+        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);
     }
 }

+ 3 - 0
fhKeeper/formulahousekeeper/ops-platform/src/main/resources/application.yml

@@ -82,6 +82,9 @@ mybatis:
 upload:
   path: C:/upload/
 
+logDownLoad:
+  path: C:/upload/wt_print.log
+
 ##actuator健康检查配置
 management:
   security: