diff --git a/ecp-mbu-web/src/main/java/com/murong/ecp/netpay/mbu/web/controller/FileUploadController.java b/ecp-mbu-web/src/main/java/com/murong/ecp/netpay/mbu/web/controller/FileUploadController.java new file mode 100644 index 00000000..3348d568 --- /dev/null +++ b/ecp-mbu-web/src/main/java/com/murong/ecp/netpay/mbu/web/controller/FileUploadController.java @@ -0,0 +1,99 @@ +package com.murong.ecp.netpay.mbu.web.controller; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.yuangou.ecp.bp.core.common.util.YGECPProperty; +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; + +@RestController +@RequestMapping("/file") +public class FileUploadController { + private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadController.class); + + @Autowired + private Environment env; + + @GetMapping("/upload") + public String upload() { + return "upload"; + } + + @RequestMapping({"/upload.do"}) + public JSONObject handleFileUpload(HttpServletRequest request) { + Map fileMap = ((MultipartHttpServletRequest)request).getFileMap(); + String applicationName = this.env.getProperty("spring.application.name"); + String filePath = YGECPProperty.getWorkDir() + "/data/" + applicationName; + File filePathFile = new File(filePath); + if (!filePathFile.exists()) { + filePathFile.mkdirs(); + } + + JSONArray filsJsonArray = new JSONArray(); + Iterator var7 = fileMap.keySet().iterator(); + + while(var7.hasNext()) { + String key = (String)var7.next(); + MultipartFile file = (MultipartFile)fileMap.get(key); + if (file.isEmpty()) { + LOGGER.info("文件: " + key + " 上传失败因为文件为空"); + return this.getErrorResult("文件: " + key + " 上传失败因为文件为空"); + } + + try { + String fileName = file.getOriginalFilename(); + fileName = UUID.randomUUID().toString() + "." + this.getFileSuffix(fileName); + File dest = new File(filePath + "/" + fileName); + FileUtils.copyInputStreamToFile(file.getInputStream(), dest); + JSONObject filJson = new JSONObject(); + filJson.put("file_name", fileName); + filsJsonArray.add(filJson); + } catch (Exception var13) { + LOGGER.info("文件: " + key + " 上传失败 ==> " + var13.getMessage(), var13); + return this.getErrorResult("文件: " + key + " 上传失败 ==> " + var13.getMessage()); + } + } + + JSONObject result = this.getSuccResult(); + result.put("file_list", filsJsonArray); + return result; + } + + private JSONObject getErrorResult(String msgInf) { + JSONObject gda = new JSONObject(); + JSONObject result = new JSONObject(); + gda.put("msg_typ", "E"); + gda.put("msg_cd", "SCM00010"); + gda.put("msg_inf", msgInf); + result.put("gda", gda); + return result; + } + + private JSONObject getSuccResult() { + JSONObject gda = new JSONObject(); + JSONObject result = new JSONObject(); + gda.put("msg_typ", "N"); + gda.put("msg_cd", "SCM00000"); + result.put("gda", gda); + return result; + } + + private String getFileSuffix(String fileName) { + int idx = fileName.lastIndexOf("."); + return idx == -1 ? "" : fileName.substring(idx + 1); + } + +}