Commit 620eb6c0 by liuyang

2024-07-24 app文件上传

parent 6743e1a7
......@@ -18,6 +18,8 @@ import java.util.Map;
import net.sf.json.JSONObject;
import javax.servlet.http.HttpServletRequest;
/**
* @author:songx
* @date:2024/3/14,14:26
......@@ -34,10 +36,11 @@ public class FileUploadController {
* @throws IOException
*/
@RequestMapping(value = "/app", method = RequestMethod.POST)
public Map upload(MultipartFile file) throws IOException {
public Map upload(MultipartFile file, HttpServletRequest request) throws IOException {
Map resultMap = new HashMap();
try {
JSONObject upload = UploadUtils.upload((CommonsMultipartFile)file);
//JSONObject upload = UploadUtils.upload((CommonsMultipartFile)file);
JSONObject upload = UploadUtils.uploadFile(request);
resultMap.put("id", upload.getString("docId"));
resultMap.put("status", EiConstant.STATUS_SUCCESS);
resultMap.put("message", "SUCCESS");
......
package com.baosight.hggp.hg.mt.utils;
import com.baosight.iplat4j.core.data.ibatis.dao.Dao;
import com.baosight.iplat4j.core.data.id.UUIDHexIdGenerator;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.ioc.spring.PlatApplicationContext;
import com.baosight.iplat4j.core.log.Logger;
import com.baosight.iplat4j.core.log.LoggerFactory;
import com.baosight.iplat4j.core.service.soa.XServiceManager;
import com.baosight.iplat4j.core.util.DateUtils;
import com.baosight.iplat4j.core.util.StringUtils;
import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import com.baosight.iplat4j.eu.dm.PlatFileUploadManager;
import com.baosight.iplat4j.eu.dm.PlatFileUploadUtils;
import com.baosight.iplat4j.eu.dm.util.IFileUploader;
import net.sf.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.io.File;
import java.util.*;
/**
* @author LiuYang
......@@ -16,6 +33,8 @@ import java.util.Map;
*/
public class UploadUtils {
private static final Logger logger = LoggerFactory.getLogger(UploadUtils.class);
private static Dao dao = (Dao) PlatApplicationContext.getApplicationContext().getBean("dao");
public static JSONObject upload(CommonsMultipartFile multipartFile ) throws Exception {
JSONObject documentJSON = new JSONObject();
......@@ -43,4 +62,165 @@ public class UploadUtils {
}
public static JSONObject uploadFile(HttpServletRequest request) throws Exception {
new EiInfo();
IFileUploader fileUploader = PlatApplicationContext.getApplicationContext().getBean("platFileUploader", IFileUploader.class);
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
String dirEname = request.getParameter("path");
String docTag = request.getParameter("docTag");
String ename = request.getParameter("ename");
Boolean isToEs = request.getParameter("isToEs") == null ? false : Boolean.valueOf(request.getParameter("isToEs"));
boolean isTemp = "__temp__".equals(docTag);
UserSession.web2Service(request);
String loginId = UserSession.getUserId();
String loginCName = UserSession.getLoginCName();
JSONObject documentJSON = new JSONObject();
//MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
Iterator multiFileIterator = ((DefaultMultipartHttpServletRequest) request).getMultiFileMap().get("uploadFile").iterator();
String chgName;
String filePath;
String msg;
String category;
String docPath;
while(multiFileIterator.hasNext()) {
CommonsMultipartFile multipartFile = (CommonsMultipartFile)multiFileIterator.next();
filePath = multipartFile.getOriginalFilename();
Long docSize = multipartFile.getSize();
chgName = fileUploader.transformFileName(filePath);
String attachmentName;
if (isTemp) {
File dir = new File(dirEname);
if (!dir.exists()) {
dir.mkdirs();
}
attachmentName = dirEname + "/" + filePath;
multipartFile.transferTo(new File(attachmentName));
documentJSON.put("docId", filePath);
documentJSON.put("docTag", docTag);
documentJSON.put("author", loginId);
documentJSON.put("authorCName", loginCName);
documentJSON.put("uploadTime", DateUtils.toDateTimeStr14(new Date()));
documentJSON.put("docUrl", attachmentName);
documentJSON.put("docSize", docSize);
documentJSON.put("docName", filePath);
} else {
Map<String, String> pathMap = fileUploader.getRealPath(dirEname);
msg = (String)pathMap.get("dirId");
category = (String)pathMap.get("path");
checkInvalidPath(category);
File dir = new File(category);
if (!dir.exists()) {
dir.mkdirs();
}
docPath = UUIDHexIdGenerator.generate().toString();
Map map = new HashMap();
map.put("docId", docPath);
map.put("dirId", msg);
map.put("docTag", docTag);
map.put("docName", filePath);
map.put("chgName", chgName);
map.put("docSize", docSize);
map.put("recCreator", loginId);
map.put("recCreateTime", DateUtils.curDateTimeStr14());
fileUploader.uploadDoc(map);
attachmentName = category + "/" + chgName;
multipartFile.transferTo(new File(attachmentName));
documentJSON.put("docId", docPath);
documentJSON.put("docTag", docTag);
documentJSON.put("author", loginId);
documentJSON.put("authorCName", loginCName);
documentJSON.put("uploadTime", DateUtils.toDateTimeStr14(new Date()));
documentJSON.put("docUrl", attachmentName);
documentJSON.put("docSize", docSize);
documentJSON.put("docName", filePath);
}
}
if (isToEs && !isTemp) {
String docId = documentJSON.get("docId").toString();
EiInfo initTag = new EiInfo();
if (docTag.isEmpty()) {
initTag.set("docTag", "root");
} else {
initTag.set("docTag", docTag);
}
initTag.set(EiConstant.serviceId, "S_EU_DC_00");
EiInfo initTagResult = XServiceManager.call(initTag);
Integer status = initTagResult.getStatus();
msg = initTagResult.getMsg();
chgName = null;
if (initTagResult.getStatus() >= 0) {
category = initTagResult.get("dirId").toString();
chgName = documentJSON.get("author").toString();
docPath = documentJSON.get("docName").toString();
logger.info("文件上传进Es库,文件名:" + dirEname);
EiInfo updateToEs = new EiInfo();
updateToEs.set("category", category);
updateToEs.set("docId", docId);
updateToEs.set("username", chgName);
updateToEs.set("docName", docPath);
updateToEs.set(EiConstant.serviceId, "S_EU_DC_01");
EiInfo updateToEsResult = XServiceManager.call(updateToEs);
status = updateToEsResult.getStatus();
msg = updateToEsResult.getMsg();
if (status >= 0) {
HashMap fileMap = (HashMap)updateToEsResult.get("fileMap");
HashMap docMap = (HashMap)updateToEsResult.get("docMap");
String newdocId = docMap.get("docId").toString();
String createTime = docMap.get("createTime").toString();
String physicalPath = fileMap.get("physicalPath").toString();
String cfgPath = updateToEsResult.get("cfgPath").toString();
documentJSON.put("docId", newdocId);
documentJSON.put("uploadTime", createTime);
documentJSON.put("docUrl", cfgPath + physicalPath);
}
}
Map document = fileUploader.getDocument(docId);
chgName = document.get("chgName").toString();
docPath = document.get("realPath").toString();
filePath = docPath + chgName;
if (fileUploader.deleteDocById(docId) > 0) {
deleteDoc(filePath);
}
if (status < 0) {
logger.info("文件上传进Es库失败" + msg);
throw new PlatException(msg);
}
}
return documentJSON;
}
private static void checkInvalidPath(String realPath) {
String pathErrorMsg = "Upload params contain invalid characters!";
if (realPath.contains("../")) {
throw new PlatException(pathErrorMsg);
} else {
String invalidPathConfig = PlatApplicationContext.getProperty("iplat.upload.invalidPath");
if (StringUtils.isNotEmpty(invalidPathConfig)) {
String[] invalidPaths = invalidPathConfig.split(",");
String[] var5 = invalidPaths;
int var6 = invalidPaths.length;
for(int var7 = 0; var7 < var6; ++var7) {
String invalidPath = var5[var7];
if (realPath.contains(invalidPath)) {
throw new PlatException(pathErrorMsg);
}
}
}
}
}
public static boolean deleteDoc(String filePath) {
File file = new File(filePath);
return file.exists() && file.delete();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment