From c92ef51676efa03d41c1cf7b94f4077d58a7babd Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期一, 21 四月 2025 14:25:29 +0800 Subject: [PATCH] 代码初始化 --- server/src/main/java/com/doumee/api/common/PublicCloudController.java | 127 +++++++++++++++++++++++++++++++++++++++--- 1 files changed, 118 insertions(+), 9 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 4ecc8da..bc71f5b 100644 --- a/server/src/main/java/com/doumee/api/common/PublicCloudController.java +++ b/server/src/main/java/com/doumee/api/common/PublicCloudController.java @@ -10,16 +10,16 @@ import com.doumee.core.exception.BusinessException; import com.doumee.core.utils.DateUtil; import com.doumee.core.utils.FtpUtil; +import com.doumee.core.utils.azure.AzureBlobUtil; import com.doumee.dao.system.model.SystemDictData; +import com.doumee.service.common.EmailService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +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.commons.CommonsMultipartResolver; @@ -44,15 +44,38 @@ public class PublicCloudController extends BaseController { @Autowired private SystemDictDataBiz systemDictDataBiz; + @Autowired + private EmailService emailService; + @Value("${upload.type") + private String uploadType; public static FtpUtil ftp = null; + public static AzureBlobUtil azureBlobUtil = null; + @ApiOperation(value = "娴嬭瘯閭欢鍙戦��") + @RequestMapping(method= RequestMethod.POST,value="/testEmail") + @ResponseBody + public void test( @RequestParam(required = false) String id, @RequestParam(required = false) String email) throws Exception { +// sendEmailWithImages(String toEmail, String title, Map<String,String> contentForm, List<String> imgList) + Map<String,String> content = new HashMap<>(); + content.put("鏍囬","杩欐槸鏍囬"); + content.put("绠�浠�","杩欐槸绠�浠�"); + List<String> list = new ArrayList<>(); + list.add("https://dmtest.ahapp.net/file/workorder/20250410/1.jpg"); + list.add("https://dmtest.ahapp.net/file/workorder/20250410/2.png"); + list.add("https://dmtest.ahapp.net/file/workorder/20250410/4.png"); + emailService.sendEmailWithImages(StringUtils.defaultString(email,"jp@doumee.com"),"鏉ヨ嚜韫勮箘鐨勯偖浠�"+DateUtil.getPlusTime2(new Date()),content,list); + + } @ApiOperation(value = "鎵归噺涓婁紶鏂囦欢鍒癋TP") @RequestMapping(method= RequestMethod.POST,value="/uploadBatch") @ResponseBody - public void uploadBatch(HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { + public void uploadBatch(@RequestParam("files") MultipartFile[] fileList,HttpServletRequest request, HttpServletResponse response, String folder) throws Exception { if(Objects.isNull(folder)){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + if(Objects.isNull(fileList)){ throw new BusinessException(ResponseStatus.BAD_REQUEST); } SystemDictData folderData = systemDictDataBiz.queryByCode(Constants.FTP,folder); @@ -77,13 +100,70 @@ context.put("success", true); context.put("code", 200); context.put("errno",0); + JSONArray jsonArray = new JSONArray(); + if (fileList.length > 0) { + //閬嶅巻鏂囦欢鍒楄〃 + Integer index = Constants.ONE; + for (MultipartFile file :fileList) { + //鑾峰緱姣忎竴涓枃浠� + 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.length),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(); @@ -97,7 +177,12 @@ 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); + boolean r =false; + if(StringUtils.equals(Constants.uploadType.blob,uploadType)){ + r = azureBlobUtil.uploadFileWithResponse( fileName,is);//涓婁紶 + }else{ + r = ftp.uploadInputstreamBatch(is, fileName, Constants.equalsInteger(index ,fileList.size()),index); + } if (r) { JSONObject fileJSON = new JSONObject(); fileJSON.put("url", prefixPath + fileName); @@ -110,7 +195,6 @@ } } - // while (it.hasNext()) { // MultipartFile file = multipartRequest.getFile(it.next()); // String originname = file.getOriginalFilename(); @@ -144,6 +228,26 @@ 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{ + 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") @RequestMapping(method= RequestMethod.POST,value="/upload") @ResponseBody @@ -151,7 +255,11 @@ // folder = systemDictDataBiz.queryByCode(Constants.FTP,folder).getCode(); Date d1 = new Date(); log.error("鎬诲緱涓婁紶鏂囦欢鎴愬姛=============寮�濮�========="+DateUtil.getPlusTime2(d1)); - folder =StringUtils.defaultString(folder).replace("/", ""); + SystemDictData folderData = systemDictDataBiz.queryByCode(Constants.FTP,folder); + if(Objects.isNull(folderData)||StringUtils.isBlank(folderData.getCode())){ + throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鐩爣鏂囦欢澶归敊璇�"); + } + folder =folderData.getCode().replace("/", ""); String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); InputStream is = null; response.setCharacterEncoding("UTF-8"); @@ -173,6 +281,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"; -- Gitblit v1.9.3