Browse Source

Merge branch 'feature-way-of-configuration' into beta

sunyj 7 years ago
parent
commit
7c19df3eec

+ 2 - 2
report-common/src/main/java/com/uas/report/util/FileUtils.java

@@ -54,7 +54,7 @@ public class FileUtils {
 		}
 
 		File file = new File(filePath);
-		if (!file.getParentFile().exists()) {
+		if (file.getParentFile() != null && !file.getParentFile().exists()) {
 			file.getParentFile().mkdirs();
 		}
 		FileOutputStream fos = new FileOutputStream(file);
@@ -74,7 +74,7 @@ public class FileUtils {
 	 * @return 写入的文件
 	 */
 	public static File write(File file, String content) throws IOException {
-		if (!file.getParentFile().exists()) {
+		if (file.getParentFile() != null && !file.getParentFile().exists()) {
 			file.getParentFile().mkdirs();
 		}
 		file.createNewFile();

+ 2 - 2
report-common/src/main/java/com/uas/report/util/ZipUtils.java

@@ -146,7 +146,7 @@ public class ZipUtils {
 				}
 				continue;
 			}
-			if (!outFile.getParentFile().exists()) {
+			if (outFile.getParentFile() != null && !outFile.getParentFile().exists()) {
 				outFile.getParentFile().mkdirs();
 			}
 
@@ -199,7 +199,7 @@ public class ZipUtils {
 			}
 			InputStream inputStream = zipFile.getInputStream(zipEntry);
 			File outFile = new File(folder.getPath() + File.separator + zipEntry.getName());
-			if (!outFile.getParentFile().exists()) {
+			if (outFile.getParentFile() != null && !outFile.getParentFile().exists()) {
 				outFile.getParentFile().mkdirs();
 			}
 

+ 1 - 1
report/src/main/java/com/uas/report/Application.java

@@ -41,7 +41,7 @@ public class Application extends SpringBootServletInitializer implements Applica
 	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
 		try {
 			File logFile = new File("logs/log.log");
-			if (!logFile.getParentFile().exists()) {
+			if (logFile.getParentFile() != null && !logFile.getParentFile().exists()) {
 				logFile.getParentFile().mkdir();
 			}
 			System.setErr(new PrintStream(new FileOutputStream(logFile, true)));

+ 19 - 2
report/src/main/java/com/uas/report/DynamicProperties.java

@@ -3,13 +3,17 @@ package com.uas.report;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.report.annotation.DynamicValue;
 import com.uas.report.model.ExportType;
-import com.uas.report.util.*;
+import com.uas.report.util.FileUtils;
+import com.uas.report.util.ObjectUtils;
+import com.uas.report.util.ResourceUtils;
+import com.uas.report.util.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
 import java.io.*;
 import java.lang.reflect.Field;
+import java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -130,9 +134,22 @@ public class DynamicProperties {
                     if (defaultPropertyFile == null) {
                         throw new FileNotFoundException("配置文件不存在:" + PROPERTY_FILE_NAME);
                     }
-                    FileUtils.copy(defaultPropertyFile, propertyFile);
+                    // 是否以 spring boot jar 形式运行
+                    URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
+                    if (org.springframework.util.ResourceUtils.isJarURL(location)) {
+                        // 以 spring boot jar 形式运行时,不能直接复制默认配置文件,需以输入流的方式读取默认配置
+                        InputStream inputStream = getClass().getResourceAsStream("/" + PROPERTY_FILE_NAME);
+                        byte[] data = new byte[inputStream.available()];
+                        inputStream.read(data);
+                        FileUtils.write(propertyFile.getPath(), data);
+                    }else{
+                        FileUtils.copy(defaultPropertyFile, propertyFile);
+                    }
                 }
             }
+            if(propertyFile.isDirectory()){
+                throw new IOException("并非文件:" + PROPERTY_FILE_NAME);
+            }
             long lastModified = propertyFile.lastModified();
             // 如果配置文件有修改,就重新加载
             if (propertyFileLastModified >= lastModified) {

+ 2 - 2
report/src/main/java/com/uas/report/service/impl/FileServiceImpl.java

@@ -138,7 +138,7 @@ public class FileServiceImpl implements FileService {
 		stringBuilder.append(fileName);
 		String targetFilePath = stringBuilder.toString();
 		final File targetFile = new File(targetFilePath);
-		if (!targetFile.getParentFile().exists()) {
+		if (targetFile.getParentFile() != null && !targetFile.getParentFile().exists()) {
 			targetFile.getParentFile().mkdirs();
 		}
 		try {
@@ -159,7 +159,7 @@ public class FileServiceImpl implements FileService {
 		filePath = getAbsolutePath(filePath, isAbsolutePath);
 		File targetFile = new File(filePath + "/" + files[0].getOriginalFilename());
 		// 检查路径是否存在
-		if (!targetFile.getParentFile().exists()) {
+		if (targetFile.getParentFile() != null && !targetFile.getParentFile().exists()) {
 			targetFile.getParentFile().mkdirs();
 		}
 

+ 1 - 1
report/src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -69,7 +69,7 @@ public class PrintServiceImpl implements PrintService {
 			throw new SQLException("获取数据源失败");
 		}
 
-		if(!exportFile.getParentFile().exists()){
+		if(exportFile.getParentFile() != null && !exportFile.getParentFile().exists()){
             exportFile.getParentFile().mkdirs();
         }