Ver código fonte

提交丢失的文件

QuYueTing 1 mês atrás
pai
commit
436fda786d

+ 102 - 0
fhKeeper/formulahousekeeper/ops-platform/src/main/java/com/management/platform/controller/CommonUploadController.java

@@ -0,0 +1,102 @@
+package com.management.platform.controller;
+
+import com.management.platform.entity.Company;
+import com.management.platform.util.HttpRespMsg;
+import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
+import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
+import fr.opensagres.xdocreport.document.IXDocReport;
+import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
+import fr.opensagres.xdocreport.template.IContext;
+import fr.opensagres.xdocreport.template.TemplateEngineKind;
+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;
+
+@RestController
+@RequestMapping("/common")
+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) {
+        HttpRespMsg msg = new HttpRespMsg();
+
+        //然后处理文件
+        String fileName = multipartFile.getOriginalFilename();
+        String[] split = fileName.split("\\.");
+        String serverName = UUID.randomUUID().toString().replaceAll("-", "") + "."+split[split.length-1];
+
+        //检查目录
+        File dir = new File(path);
+        if (!dir.exists()) {
+            dir.mkdir();
+        }
+        File file = new File(dir, serverName);
+        InputStream inputStream = null;
+        OutputStream outputStream = null;
+        try {
+            inputStream = multipartFile.getInputStream();
+            outputStream = new FileOutputStream(file);
+            byte[] buffer = new byte[4096];
+            int temp = 0;
+            while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
+                outputStream.write(buffer, 0, temp);
+            }
+            inputStream.close();
+            outputStream.close();
+            msg.data = serverName;
+        } catch (Exception exception) {
+            exception.printStackTrace();
+            logger.error(exception.getMessage());
+        }
+
+        return msg;
+    }
+    @RequestMapping("/downLoadLog")
+    public ResponseEntity<byte[]> downLoadLog() throws IOException {
+        // 🧐🧐🧐读取本地的文件
+        // 获取File对象
+        System.out.println("读取目录=="+logDownLoadPath);
+        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);
+    }
+}