|
|
@@ -2,7 +2,10 @@ package com.uas.report.controller;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
@@ -18,6 +21,15 @@ public class UploadController {
|
|
|
|
|
|
private Logger logger = Logger.getLogger(UploadController.class);
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param userName
|
|
|
+ * 账套名,文件上传至相应账套路径下
|
|
|
+ * @param file
|
|
|
+ * 上传的文件
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@RequestMapping("")
|
|
|
@ResponseBody
|
|
|
public String upload(String userName, MultipartFile file) {
|
|
|
@@ -35,24 +47,47 @@ public class UploadController {
|
|
|
}
|
|
|
|
|
|
logger.info("开始上传...");
|
|
|
- String reportName = file.getOriginalFilename();
|
|
|
- String targetFilePath = new StringBuilder(ReportConstants.REPORT_DIR).append(userName).append(File.separator)
|
|
|
- .append(reportName).toString();
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ StringBuilder stringBuilder = new StringBuilder(ReportConstants.REPORT_DIR).append(userName)
|
|
|
+ .append(File.separator);
|
|
|
+ // 上传图片
|
|
|
+ if (isPicture(fileName)) {
|
|
|
+ stringBuilder.append("Picture").append(File.separator);
|
|
|
+ }
|
|
|
+ stringBuilder.append(fileName);
|
|
|
+ String targetFilePath = stringBuilder.toString();
|
|
|
File targetFile = new File(targetFilePath);
|
|
|
if (!targetFile.exists()) {
|
|
|
targetFile.mkdirs();
|
|
|
}
|
|
|
try {
|
|
|
file.transferTo(targetFile);
|
|
|
- message = "成功上传文件 " + reportName + " 至 " + targetFile.getCanonicalPath();
|
|
|
+ message = "成功上传文件 " + fileName + " 至 " + targetFile.getCanonicalPath();
|
|
|
logger.info(message);
|
|
|
return message;
|
|
|
} catch (IllegalStateException | IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- message = "上传文件 " + reportName + " 失败";
|
|
|
+ message = "上传文件失败: " + fileName;
|
|
|
logger.error(message);
|
|
|
return message;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否为图片
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean isPicture(String fileName) {
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<String> pictureTypes = new ArrayList<>();
|
|
|
+ CollectionUtils.addAll(pictureTypes,
|
|
|
+ new String[] { "bmp", "png", "jpg", "jpeg", "gif", "tiff", "psd", "swf", "svg" });
|
|
|
+ String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ return pictureTypes.contains(fileType);
|
|
|
+ }
|
|
|
}
|