java110 лет назад: 2
Родитель
Сommit
ded40ebbd1

+ 34 - 24
service-api/src/main/java/com/java110/api/smo/file/impl/AddFileSMOImpl.java

@@ -37,37 +37,47 @@ public class AddFileSMOImpl extends DefaultAbstractComponentSMO implements IAddF
 
     @Override
     public ResponseEntity<String> saveFile(IPageData pd, MultipartFile uploadFile) throws IOException {
-
-        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
-        if (uploadFile.getSize() > 2 * 1024 * 1024) {
-            throw new IllegalArgumentException("上传文件超过两兆");
+        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());
+
+            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(MappingConstant.FILE_DOMAIN,"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");
@@ -87,7 +97,7 @@ public class AddFileSMOImpl extends DefaultAbstractComponentSMO implements IAddF
 
         JSONObject outParam = new JSONObject();
         outParam.put("fileId", fileName);
-        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"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);

+ 32 - 38
springboot/src/main/java/com/java110/boot/smo/file/impl/AddFileSMOImpl.java

@@ -41,46 +41,40 @@ public class AddFileSMOImpl extends DefaultAbstractComponentSMO implements IAddF
 
     @Override
     public ResponseEntity<String> saveFile(IPageData pd, MultipartFile uploadFile) throws IOException {
-
-        JSONObject paramIn = JSONObject.parseObject(pd.getReqData());
-        if (uploadFile.getSize() > 2 * 1024 * 1024) {
-            throw new IllegalArgumentException("上传文件超过两兆");
+        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());
+
+            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, "communityId", "必填,请填写小区ID");
-        Assert.hasKeyAndValue(paramIn, "suffix", "必填,请填写文件类型");
-        super.checkUserHasPrivilege(pd, restTemplate, PrivilegeCodeConstant.SAVE_FILE);
-
-        ComponentValidateResult result = this.validateStoreStaffCommunityRelationship(pd, restTemplate);
-        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.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);
-
-//
-//        String apiUrl = "file.saveFile" ;
-//
-//
-//        ResponseEntity<String> responseEntity = this.callCenterService(restTemplate, pd, paramIn.toJSONString(),
-//                apiUrl,
-//                HttpMethod.POST);
-        return responseEntity;
-
     }
 
     @Override