From ded40ebbd1613490fc68190dbd92db5d7243267f Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期二, 22 八月 2023 16:25:20 +0800
Subject: [PATCH] 优化代码
---
service-api/src/main/java/com/java110/api/smo/file/impl/AddFileSMOImpl.java | 59 ++++++++++++++++++++++++++++++++++-------------------------
1 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/service-api/src/main/java/com/java110/api/smo/file/impl/AddFileSMOImpl.java b/service-api/src/main/java/com/java110/api/smo/file/impl/AddFileSMOImpl.java
index 60a0a93..99180d9 100644
--- a/service-api/src/main/java/com/java110/api/smo/file/impl/AddFileSMOImpl.java
+++ b/service-api/src/main/java/com/java110/api/smo/file/impl/AddFileSMOImpl.java
@@ -5,11 +5,10 @@
import com.java110.core.context.IPageData;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.file.FileDto;
-import com.java110.entity.component.ComponentValidateResult;
import com.java110.api.smo.file.IAddFileSMO;
import com.java110.intf.common.IFileInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
-import com.java110.utils.constant.PrivilegeCodeConstant;
+import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.Assert;
import com.java110.utils.util.Base64Convert;
import com.java110.utils.util.BeanConvertUtil;
@@ -38,37 +37,47 @@
@Override
public ResponseEntity<String> saveFile(IPageData pd, MultipartFile uploadFile) throws IOException {
+ InputStream is = null;
+ try {
+ JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
+ if (uploadFile.getSize() > 2 * 1024 * 1024) {
+ throw new IllegalArgumentException("涓婁紶鏂囦欢瓒呰繃涓ゅ厗");
+ }
+ Assert.hasKeyAndValue(paramIn, "suffix", "蹇呭~锛岃濉啓鏂囦欢绫诲瀷");
+ is = uploadFile.getInputStream();
+ String fileContext = Base64Convert.ioToBase64(is);
+ paramIn.put("context", fileContext);
+ paramIn.put("fileName", uploadFile.getOriginalFilename());
- JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
- if (uploadFile.getSize() > 2 * 1024 * 1024) {
- throw new IllegalArgumentException("涓婁紶鏂囦欢瓒呰繃涓ゅ厗");
+ FileDto fileDto = BeanConvertUtil.covertBean(paramIn, FileDto.class);
+ fileDto.setCommunityId("-1");
+ fileDto.setFileId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
+
+ String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
+
+ JSONObject outParam = new JSONObject();
+ outParam.put("fileId", fileName);
+ String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
+ outParam.put("url", imgUrl + fileName);
+
+ ResponseEntity<String> responseEntity = new ResponseEntity<String>(outParam.toJSONString(), HttpStatus.OK);
+ return responseEntity;
+ } finally {
+ if (is != null) {
+ try {
+ is.close();
+ } catch (Exception e) {
+ }
+ }
}
- Assert.hasKeyAndValue(paramIn, "suffix", "蹇呭~锛岃濉啓鏂囦欢绫诲瀷");
- InputStream is = uploadFile.getInputStream();
- String fileContext = Base64Convert.ioToBase64(is);
- paramIn.put("context", fileContext);
- paramIn.put("fileName", uploadFile.getOriginalFilename());
- FileDto fileDto = BeanConvertUtil.covertBean(paramIn, FileDto.class);
- fileDto.setCommunityId("-1");
- fileDto.setFileId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_file_id));
-
- String fileName = fileInnerServiceSMOImpl.saveFile(fileDto);
-
- JSONObject outParam = new JSONObject();
- outParam.put("fileId", fileName);
- String imgUrl = MappingCache.getValue("IMG_PATH");
- outParam.put("url", imgUrl + fileName);
-
- ResponseEntity<String> responseEntity = new ResponseEntity<String>(outParam.toJSONString(), HttpStatus.OK);
- return responseEntity;
}
@Override
public ResponseEntity<String> savePhotoFile(IPageData pd) {
String images = pd.getReqData();
- JSONObject imageObj=JSONObject.parseObject(images);
+ JSONObject imageObj = JSONObject.parseObject(images);
JSONObject paramIn = new JSONObject();
paramIn.put("context", imageObj.getString("uploadFile"));
paramIn.put("fileName", "upload.jpg");
@@ -88,7 +97,7 @@
JSONObject outParam = new JSONObject();
outParam.put("fileId", fileName);
- String imgUrl = MappingCache.getValue("IMG_PATH");
+ String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
outParam.put("url", imgUrl + fileName);
ResponseEntity<String> responseEntity = new ResponseEntity<String>(outParam.toJSONString(), HttpStatus.OK);
--
Gitblit v1.8.0