diff --git a/adc-da-main/src/main/resources/application-dev.properties b/adc-da-main/src/main/resources/application-dev.properties index 858d3b9..6db4219 100644 --- a/adc-da-main/src/main/resources/application-dev.properties +++ b/adc-da-main/src/main/resources/application-dev.properties @@ -61,8 +61,13 @@ swaggerLevel=0 # #picPath=/EPR/file/home/pic/ +# \u672C\u5730\u6587\u4EF6\u5B58\u50A8\u8DEF\u5F84\u65B9\u5F0F jar\u5305\u8DEF\u5F84\uFF1Ajar \u5DE5\u4F5C\u76EE\u5F55\uFF1Auserdir \u56FA\u5B9A\u76EE\u5F55\uFF1Asettled +localSaveFileType=settled +#localSaveFileType=userdir + # \u672C\u5730\u5B58\u50A8\u6587\u4EF6\u7684\u78C1\u76D8\u5730\u5740 -uploadFile=D:\\work\\idea\\changan\\ +#uploadFile=D:\\work\\idea\\changan\\ +uploadFile=/data/DeploymentPackage/report_library/uploadfile # \u672C\u5730\u6587\u4EF6\u8BBF\u95EE\u7684url\u5730\u5740 picPath=/api/home/pic/ diff --git a/adc-da-report/src/main/java/com/adc/da/report/util/LocalFileUtil.java b/adc-da-report/src/main/java/com/adc/da/report/util/LocalFileUtil.java new file mode 100644 index 0000000..9e58c73 --- /dev/null +++ b/adc-da-report/src/main/java/com/adc/da/report/util/LocalFileUtil.java @@ -0,0 +1,73 @@ +package com.adc.da.report.util; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.*; +import java.net.URISyntaxException; + +/** + * @author Caihaohan + */ +@Slf4j +@Component +public class LocalFileUtil { + + @Value("${uploadFile}") + private String uploadFile; + + @Value("${localSaveFileType}") + private String localSaveFileType; + + /** + * 获取当前Jar包的路径 + * @return jar包路径 + */ + private String getJarPath() throws URISyntaxException { + return new File(LocalFileUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath(); + } + + /** + * 获取当前工作目录路径 + * @return 当前工作目录路径 + */ + private String getUserDir() { + return System.getProperty("user.dir"); + } + + /** + * 按照配置文件里指定的方式返回对应路径 + * @return + */ + private String getPath() { + try { + switch (localSaveFileType) { + case "userdir": + return getUserDir(); + case "settled": + return uploadFile; + case "jar": + return getJarPath(); + default: + } + } catch (URISyntaxException e) { + log.info("获取Jar包路径失败,返回固定路径", e); + } + return uploadFile; + } + + public void saveFile(InputStream fileInputStream, String fileName) throws IOException { + // 获取当前工作目录并拼接文件名 + String path = getPath() + File.separator + fileName; + FileOutputStream fileOutputStream = new FileOutputStream(path); + byte[] buffer = new byte[1024]; + int length; + while ((length = fileInputStream.read(buffer)) > 0) { + fileOutputStream.write(buffer, 0, length); + } + fileInputStream.close(); + fileOutputStream.close(); + } + +} \ No newline at end of file