From 30e858fa504b268b9b436afca0a1259cf6e8c488 Mon Sep 17 00:00:00 2001 From: MrShi <1878285526@qq.com> Date: 星期二, 19 八月 2025 11:01:40 +0800 Subject: [PATCH] 优化 --- server/src/main/java/com/doumee/api/common/PublicCloudController.java | 357 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 288 insertions(+), 69 deletions(-) diff --git a/server/src/main/java/com/doumee/api/common/PublicCloudController.java b/server/src/main/java/com/doumee/api/common/PublicCloudController.java index e2a317a..2f51442 100644 --- a/server/src/main/java/com/doumee/api/common/PublicCloudController.java +++ b/server/src/main/java/com/doumee/api/common/PublicCloudController.java @@ -4,12 +4,19 @@ import com.alibaba.fastjson.JSONObject; import com.doumee.api.BaseController; import com.doumee.biz.system.SystemDictDataBiz; +import com.doumee.config.annotation.EncryptionReq; +import com.doumee.config.annotation.EncryptionResp; import com.doumee.core.annotation.trace.Trace; import com.doumee.core.constants.Constants; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; +import com.doumee.core.model.ApiResponse; import com.doumee.core.utils.DateUtil; import com.doumee.core.utils.FtpUtil; +import com.doumee.core.utils.azure.AzureBlobUtil; +import com.doumee.core.utils.qiyeweixin.QywxUtil; +import com.doumee.core.utils.qiyeweixin.model.response.QywxUploadMediaResponse; +import com.doumee.dao.business.dto.WebQwUploadDto; import com.doumee.dao.system.model.SystemDictData; import com.doumee.service.common.EmailService; import io.swagger.annotations.Api; @@ -17,9 +24,11 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; +import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.http.HttpServletRequest; @@ -44,10 +53,79 @@ private SystemDictDataBiz systemDictDataBiz; @Autowired private EmailService emailService; + @Value("${upload.type}") + private String uploadType; public static FtpUtil ftp = null; + public static AzureBlobUtil azureBlobUtil = null; + @GetMapping("/image/{folder}/{date}/{name}") + public void getImage(@PathVariable String folder,@PathVariable String date,@PathVariable String name, HttpServletResponse response) throws IOException { + try { + initUploadTool(); + response.setContentType("image/jpeg"); // 鏍规嵁浣犵殑鍥剧墖绫诲瀷璁剧疆姝g‘鐨凪IME绫诲瀷 + log.info("===================鍥剧墖blob鍚嶇О"+folder+"/"+date+"/"+name); + azureBlobUtil.downloadBlobToStream(folder+"/"+date+"/"+name,response.getOutputStream()); + }catch (Exception e) { + e.printStackTrace(); + } + } + @ApiOperation(value = "娴嬭瘯浼佷笟绱犳潗涓婁紶涓嬭浇") + @RequestMapping(method= RequestMethod.POST,value="/testQwImg") + @ResponseBody + public ApiResponse<Map<String, Object>> testQwImg( @RequestParam(required = false) String imgurl ) throws Exception { + QywxUploadMediaResponse r = QywxUtil.uploadMedia(systemDictDataBiz.queryByCode(Constants.QYWX,Constants.QYWX_TOKEN).getCode(),"image",imgurl); + if(r!=null && r.getMedia_id()!=null){ + String folder = systemDictDataBiz.queryByCode(Constants.FTP,Constants.WORKORDER_FILE_PATH).getCode(); + String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); + Map<String, Object> context = new HashMap<>(); + try { + initUploadTool(); + context.put("success", true); + context.put("code", 200); + context.put("errno",0); + JSONArray jsonArray = new JSONArray(); + Integer index = Constants.ONE; + //鑾峰緱姣忎竴涓枃浠� + String endType = ".jpg"; + String originname = UUID.randomUUID().toString()+endType; + InputStream is = QywxUtil.getMediaInputstream(systemDictDataBiz.queryByCode(Constants.QYWX,Constants.QYWX_TOKEN).getCode(),r.getMedia_id()); + if(is == null){ + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + context.put("errno",0); + }else{ + String date = DateUtil.getNowShortDate(); + String fName = date + "/" + originname; + String fileName = folder + fName; + boolean r1 =false; + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + r1 = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//涓婁紶 + }else{ + r1 = ftp.uploadInputstreamBatch(is, fileName, true,index); + } + if (r1) { + JSONObject fileJSON = new JSONObject(); + fileJSON.put("url", prefixPath + fileName); + fileJSON.put("imgaddr", fName); + fileJSON.put("imgname", fileName); + fileJSON.put("originname", originname); + jsonArray.add(fileJSON); + } + } + context.put("data",jsonArray); + } catch (Exception e) { + log.error("銆愪笂浼燜TP澶辫触銆�======================"+e.getMessage()); + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + context.put("errno",0); + } + return ApiResponse.success(context); + } + return ApiResponse.failed("涓婁紶澶辫触"+r!=null?JSONObject.toJSONString(r):""); + + } @ApiOperation(value = "娴嬭瘯閭欢鍙戦��") @RequestMapping(method= RequestMethod.POST,value="/testEmail") @ResponseBody @@ -66,8 +144,20 @@ @ApiOperation(value = "鎵归噺涓婁紶鏂囦欢鍒癋TP") @RequestMapping(method= RequestMethod.POST,value="/uploadBatch") @ResponseBody - public void uploadBatch(HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { + public void uploadBatch(MultipartRequest request, HttpServletResponse response, String folder) throws Exception { if(Objects.isNull(folder)){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + Set<String> setList = request.getMultiFileMap().keySet(); + if(Objects.isNull(setList)){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + List<MultipartFile> fileList = new ArrayList<>(); + for(String str:setList){ + List<MultipartFile> fl = request.getMultiFileMap().get(str); + fileList.addAll(fl); + } + if(Objects.isNull(fileList)){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } SystemDictData folderData = systemDictDataBiz.queryByCode(Constants.FTP,folder); @@ -80,70 +170,40 @@ response.setContentType("text/html;charset=UTF-8"); Map<String, Object> context = new HashMap<>(); try { - if(ftp == null){ - ftp = new FtpUtil(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_HOST).getCode(), - Integer.parseInt(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PORT).getCode()), - systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_USERNAME).getCode(), - systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PWD).getCode()); - }else{ - ftp.connect(); - } - + initUploadTool(); context.put("success", true); context.put("code", 200); context.put("errno",0); - CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); - if (multipartResovler.isMultipart(request)) { - MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; - List<MultipartFile> fileList = multipartRequest.getFiles("file"); - Iterator<String> it = multipartRequest.getFileNames(); - JSONArray jsonArray = new JSONArray(); - - if (fileList.size() > 0) { - //閬嶅巻鏂囦欢鍒楄〃 - Iterator<MultipartFile> fileIte = fileList.iterator(); - Integer index = Constants.ONE; - while (fileIte.hasNext()) { - //鑾峰緱姣忎竴涓枃浠� - MultipartFile file = fileIte.next(); - String originname = file.getOriginalFilename(); - InputStream is = file.getInputStream(); - String endType = originname.substring(originname.lastIndexOf("."), originname.length()); - String date = DateUtil.getNowShortDate(); - String fName = date + "/" + UUID.randomUUID() + endType; - String fileName = folder + fName; - boolean r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); - if (r) { - JSONObject fileJSON = new JSONObject(); - fileJSON.put("url", prefixPath + fileName); - fileJSON.put("imgaddr", fName); - fileJSON.put("imgname", fileName); - fileJSON.put("originname", originname); - jsonArray.add(fileJSON); - } - index = index + 1; + JSONArray jsonArray = new JSONArray(); + if (fileList.size() > 0) { + //閬嶅巻鏂囦欢鍒楄〃 + Integer index = Constants.ONE; + for (MultipartFile file :fileList) { + //鑾峰緱姣忎竴涓枃浠� + String originname = file.getOriginalFilename(); + log.error("銆愪笂浼燜TP鎴愬姛銆�=============寮�濮�========="+index+"===="+originname); + InputStream is = file.getInputStream(); + String endType = originname.substring(originname.lastIndexOf("."), originname.length()); + String date = DateUtil.getNowShortDate(); + String fName = date + "/" + UUID.randomUUID() + endType; + String fileName = folder + fName; + boolean r =false; + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + r = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//涓婁紶 + }else{ + r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); } - + if (r) { + JSONObject fileJSON = new JSONObject(); + fileJSON.put("url", prefixPath + fileName); + fileJSON.put("imgaddr", fName); + fileJSON.put("imgname", fileName); + fileJSON.put("originname", originname); + jsonArray.add(fileJSON); + } + log.error("銆愪笂浼燜TP鎴愬姛銆�=============缁撴潫========="+index+"====="+r+"===="+fName); + index = index + 1; } - -// while (it.hasNext()) { -// MultipartFile file = multipartRequest.getFile(it.next()); -// String originname = file.getOriginalFilename(); -// is = file.getInputStream(); -// String endType = originname.substring(originname.lastIndexOf("."),originname.length()); -// String date = DateUtil.getNowShortDate(); -// String fName = date+"/"+ UUID.randomUUID()+endType; -// String fileName = folder+"/"+fName; -// boolean r = ftp.uploadInputstream(is,fileName); -// if(r){ -// JSONObject fileJSON = new JSONObject(); -// fileJSON.put("url", prefixPath+fileName); -// fileJSON.put("imgaddr", fName); -// fileJSON.put("imgname", fileName); -// fileJSON.put("originname", originname); -// jsonArray.add(fileJSON); -// } -// } context.put("data",jsonArray); } } catch (Exception e) { @@ -157,6 +217,166 @@ } writerJson(response, context); return; + } + @ApiOperation(value = "鎵归噺浠庝紒涓氫复鏃剁礌鏉愬簱涓婁紶鏂囦欢鍒版枃浠舵湇鍔″櫒") + @PostMapping(value="/uploadBatchFromWx") + @ResponseBody + public void uploadBatchFromWx(@RequestBody WebQwUploadDto param, HttpServletResponse response) throws Exception { + if(Objects.isNull(param.getFolder()) || param.getMediaIds()==null || param.getMediaIds().size()==0){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + SystemDictData folderData = systemDictDataBiz.queryByCode(Constants.FTP,param.getFolder()); + if(Objects.isNull(folderData)||StringUtils.isBlank(folderData.getCode())){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐩爣鏂囦欢澶归敊璇�"); + } + String folder = systemDictDataBiz.queryByCode(Constants.FTP,param.getFolder()).getCode(); + String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=UTF-8"); + Map<String, Object> context = new HashMap<>(); + try { + initUploadTool(); + context.put("success", true); + context.put("code", 200); + context.put("errno",0); + JSONArray jsonArray = new JSONArray(); + Integer index = Constants.ONE; + for (String media : param.getMediaIds()) { + //鑾峰緱姣忎竴涓枃浠� + String endType = ".jpg"; + String originname = UUID.randomUUID().toString()+endType; + Map<String,Object> map = QywxUtil.getMediaInputstreamMap(systemDictDataBiz.queryByCode(Constants.QYWX,Constants.QYWX_TOKEN).getCode(),media); + if(map == null || map.get("data") == null){ + log.error("浼佷笟寰俊绱犳潗涓嬭浇澶辫触锛�========="+media); + continue; + } + log.error("浼佷笟寰俊绱犳潗涓嬭浇鎴愬姛锛�========="+media); + String date = DateUtil.getNowShortDate(); + String fName = date + "/" + originname; + String fileName = folder + fName; + boolean r =false; + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + r = azureBlobUtil.uploadFileWithResponseAndSize( fileName, (InputStream) map.get("data"),(Integer)map.get("size"));//涓婁紶 + }else{ + r = ftp.uploadInputstreamBatch( (InputStream) map.get("data"), fileName, Constants.equalsInteger(index ,param.getMediaIds().size()),index); + } + if (r) { + JSONObject fileJSON = new JSONObject(); + fileJSON.put("url", prefixPath + fileName); + fileJSON.put("imgaddr", fName); + fileJSON.put("imgname", fileName); + fileJSON.put("originname", originname); + jsonArray.add(fileJSON); + } + index = index + 1; + } + context.put("data",jsonArray); + } catch (Exception e) { + log.error("銆愪笂浼燜TP澶辫触銆�======================"+e.getMessage()); + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + context.put("errno",0); + writerJson(response, context); + return; + + } + writerJson(response, context); + return; + } + + @ApiOperation(value = "鎵归噺涓婁紶鏂囦欢鍒癋TP") + @RequestMapping(method= RequestMethod.POST,value="/uploadBatchOld") + @ResponseBody + public void uploadBatchOld( HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { + if(Objects.isNull(folder)){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + SystemDictData folderData = systemDictDataBiz.queryByCode(Constants.FTP,folder); + if(Objects.isNull(folderData)||StringUtils.isBlank(folderData.getCode())){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐩爣鏂囦欢澶归敊璇�"); + } + folder = systemDictDataBiz.queryByCode(Constants.FTP,folder).getCode(); + String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=UTF-8"); + Map<String, Object> context = new HashMap<>(); + try { + initUploadTool(); + context.put("success", true); + context.put("code", 200); + context.put("errno",0); + CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); + if (multipartResovler.isMultipart(request)) { + MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; + List<MultipartFile> fileList = multipartRequest.getFiles("file"); + Iterator<String> it = multipartRequest.getFileNames(); + JSONArray jsonArray = new JSONArray(); + if (fileList.size() > 0) { + //閬嶅巻鏂囦欢鍒楄〃 + Iterator<MultipartFile> fileIte = fileList.iterator(); + Integer index = Constants.ONE; + while (fileIte.hasNext()) { + //鑾峰緱姣忎竴涓枃浠� + MultipartFile file = fileIte.next(); + String originname = file.getOriginalFilename(); + InputStream is = file.getInputStream(); + String endType = originname.substring(originname.lastIndexOf("."), originname.length()); + String date = DateUtil.getNowShortDate(); + String fName = date + "/" + UUID.randomUUID() + endType; + String fileName = folder + fName; + boolean r =false; + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + r = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//涓婁紶 + }else{ + r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); + } + if (r) { + JSONObject fileJSON = new JSONObject(); + fileJSON.put("url", prefixPath + fileName); + fileJSON.put("imgaddr", fName); + fileJSON.put("imgname", fileName); + fileJSON.put("originname", originname); + jsonArray.add(fileJSON); + } + index = index + 1; + } + } + context.put("data",jsonArray); + } + } catch (Exception e) { + log.error("銆愪笂浼燜TP澶辫触銆�======================"+e.getMessage()); + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + context.put("errno",0); + writerJson(response, context); + return; + + } + writerJson(response, context); + return; + } + + private void initUploadTool() throws Exception{ + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + if(azureBlobUtil == null){ + azureBlobUtil = new AzureBlobUtil( + systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ACCESSNAME).getCode(), + systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ACCESSKEY).getCode(), + systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_ENDPOINT).getCode(), + systemDictDataBiz.queryByCode(Constants.AZURE_BLOB,Constants.AZURE_BLOB_CONTAINER).getCode()); + }else{ + azureBlobUtil.initClient(); + } + }else{ + if(ftp == null){ + ftp = new FtpUtil(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_HOST).getCode(), + Integer.parseInt(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PORT).getCode()), + systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_USERNAME).getCode(), + systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PWD).getCode()); + }else{ + ftp.connect(); + } + } } @ApiOperation(value = "涓婁紶鏂囦欢鍒癋TP") @@ -177,14 +397,7 @@ response.setContentType("text/html;charset=UTF-8"); Map<String, Object> context = new HashMap<>(); try { - if(ftp == null){ - ftp = new FtpUtil(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_HOST).getCode(), - Integer.parseInt(systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PORT).getCode()), - systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_USERNAME).getCode(), - systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_PWD).getCode()); - }else{ - ftp.connect(); - } + initUploadTool(); CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); if (multipartResovler.isMultipart(request)) { @@ -192,6 +405,7 @@ Iterator<String> it = multipartRequest.getFileNames(); while (it.hasNext()) { MultipartFile file = multipartRequest.getFile(it.next()); + System.out.println(file.getSize()); String originname = file.getOriginalFilename(); is = file.getInputStream(); String endType = ".jpg"; @@ -201,7 +415,12 @@ String date = DateUtil.getNowShortDate(); String fName = date+"/"+ UUID.randomUUID()+endType; String fileName = folder+"/"+fName; - boolean r = ftp.uploadInputstream(is,fileName); + boolean r =false; + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + r = azureBlobUtil.uploadFileWithResponseAndSize( fileName,is,null);//涓婁紶 + }else{ + r = ftp.uploadInputstream(is,fileName); + } if(r){ context.put("success", true); context.put("code", 200); -- Gitblit v1.9.3