Browse Source

允许上传没有内容的文件;上传多个文件时,一个上传失败,不再继续上传之后的文件

sunyj 8 years ago
parent
commit
b15205bbbe

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

@@ -165,7 +165,7 @@ public class FileServiceImpl implements FileService {
 			targetFile.getParentFile().mkdirs();
 			targetFile.getParentFile().mkdirs();
 		}
 		}
 		try {
 		try {
-			FileUtils.write(targetFile.getAbsolutePath(), file.getBytes());
+			FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
 			return "成功上传文件至:" + targetFile.getPath();
 			return "成功上传文件至:" + targetFile.getPath();
 		} catch (IllegalStateException | IOException | ReportException e) {
 		} catch (IllegalStateException | IOException | ReportException e) {
 			logger.error("", e);
 			logger.error("", e);
@@ -186,23 +186,22 @@ public class FileServiceImpl implements FileService {
 		}
 		}
 
 
 		StringBuilder stringBuilder = new StringBuilder();
 		StringBuilder stringBuilder = new StringBuilder();
-		for (MultipartFile file : files) {
-			targetFile = new File(targetFile.getParent() + "/" + file.getOriginalFilename());
-			try {
+		try {
+			for (MultipartFile file : files) {
+				String fileName = file.getOriginalFilename();
+				targetFile = new File(targetFile.getParent() + "/" + fileName);
 				// 不能使用file.transferTo(targetFile),
 				// 不能使用file.transferTo(targetFile),
 				// 因为在spring boot下上传文件时会对临时路径进行处理,
 				// 因为在spring boot下上传文件时会对临时路径进行处理,
 				// 导致最终文件路径不正确,如果手动设置临时路径为根路径,
 				// 导致最终文件路径不正确,如果手动设置临时路径为根路径,
 				// 又会因为权限问题导致文件写入失败,
 				// 又会因为权限问题导致文件写入失败,
 				// 所以自己在指定路径创建文件,而不使用transferTo方法
 				// 所以自己在指定路径创建文件,而不使用transferTo方法
-				FileUtils.write(targetFile.getAbsolutePath(), file.getBytes());
-				stringBuilder.append("成功上传文件至:");
-			} catch (IllegalStateException | IOException | ReportException e) {
-				logger.error("", e);
-				stringBuilder.append("上传文件失败:");
+				FileUtils.write(targetFile.getAbsolutePath(), file.getBytes(), true);
+				stringBuilder.append("上传成功:").append(fileName).append("\n");
 			}
 			}
-			stringBuilder.append(targetFile.getPath()).append("\n");
+			return stringBuilder.toString();
+		} catch (IllegalStateException | IOException e) {
+			throw new ReportException(e).setDetailedMessage(e);
 		}
 		}
-		return stringBuilder.toString();
 	}
 	}
 
 
 	@Override
 	@Override

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

@@ -30,9 +30,27 @@ public class FileUtils {
 	 *            数据
 	 *            数据
 	 */
 	 */
 	public static void write(String filePath, byte[] data) {
 	public static void write(String filePath, byte[] data) {
-		if (StringUtils.isEmpty(filePath) || ArrayUtils.isEmpty(data)) {
-			throw new ReportException("参数不能为空:filePath,data");
+		write(filePath, data, false);
+	}
+
+	/**
+	 * 写入文件
+	 * 
+	 * @param filePath
+	 *            文件路径
+	 * @param data
+	 *            数据
+	 * @param allowNoData
+	 *            是否允许数据为空(文件无内容)
+	 */
+	public static void write(String filePath, byte[] data, boolean allowNoData) {
+		if (StringUtils.isEmpty(filePath)) {
+			throw new ReportException("参数不能为空:filePath");
+		}
+		if (!allowNoData && ArrayUtils.isEmpty(data)) {
+			throw new ReportException("参数不能为空:data");
 		}
 		}
+
 		File file = new File(filePath);
 		File file = new File(filePath);
 		if (!file.getParentFile().exists()) {
 		if (!file.getParentFile().exists()) {
 			file.getParentFile().mkdirs();
 			file.getParentFile().mkdirs();

+ 3 - 1
src/main/webapp/resources/js/files/app.js

@@ -44,8 +44,10 @@ $("#uploadButton").click(function() {
 		},
 		},
 		error : function(xhr) {
 		error : function(xhr) {
 			console.log(xhr);
 			console.log(xhr);
-			alert(spinnerContainer, "上传失败");
 			spinner = hideLoading(spinner);
 			spinner = hideLoading(spinner);
+			alert(spinnerContainer, "上传失败");
+			// 此时可能上传的多个文件中,有部分上传成功,所以需要重新加载
+			listFiles(currentPath.value);
 		},
 		},
 		showLoading : function() {
 		showLoading : function() {
 			spinner = showLoading(spinner, spinnerContainer);
 			spinner = showLoading(spinner, spinnerContainer);

+ 1 - 1
src/main/webapp/resources/js/util/utils.js

@@ -112,7 +112,6 @@ function upload(option) {
 						option.error(xhr);
 						option.error(xhr);
 					}
 					}
 				}
 				}
-				document.body.removeChild(input);
 			}
 			}
 		}
 		}
 		// 展示加载动画
 		// 展示加载动画
@@ -127,6 +126,7 @@ function upload(option) {
 			}
 			}
 		}
 		}
 		xhr.send(formData);
 		xhr.send(formData);
+		document.body.removeChild(input);
 	}
 	}
 }
 }