|
@@ -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);
|
|
|
}
|
|
|
}
|