|
|
@@ -0,0 +1,59 @@
|
|
|
+package com.uas.report.controller;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import com.uas.report.util.ReportConstants;
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping("/upload")
|
|
|
+public class UploadController {
|
|
|
+
|
|
|
+ private Logger logger = Logger.getLogger(UploadController.class);
|
|
|
+
|
|
|
+ @RequestMapping("")
|
|
|
+ @ResponseBody
|
|
|
+ public String upload(String userName, MultipartFile file) {
|
|
|
+ String message = "";
|
|
|
+ if (StringUtils.isEmpty(userName)) {
|
|
|
+ message = "未传入当前账套用户名!";
|
|
|
+ logger.error(message);
|
|
|
+ // return message;
|
|
|
+ userName = "UAS";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (file == null || file.isEmpty()) {
|
|
|
+ message = "文件为空,无法进行上传!";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("开始上传...");
|
|
|
+ String reportName = file.getOriginalFilename();
|
|
|
+ String targetFilePath = new StringBuilder(ReportConstants.REPORT_DIR).append(userName).append(File.separator)
|
|
|
+ .append(reportName).toString();
|
|
|
+ File targetFile = new File(targetFilePath);
|
|
|
+ if (!targetFile.exists()) {
|
|
|
+ targetFile.mkdirs();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ file.transferTo(targetFile);
|
|
|
+ message = "成功上传文件 " + reportName + " 至 " + targetFile.getCanonicalPath();
|
|
|
+ logger.info(message);
|
|
|
+ return message;
|
|
|
+ } catch (IllegalStateException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ message = "上传文件 " + reportName + " 失败";
|
|
|
+ logger.error(message);
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+}
|