From 7298d5354963a88643a543b51b90192dc9fc934c Mon Sep 17 00:00:00 2001 From: doum <doum> Date: 星期四, 11 九月 2025 18:43:14 +0800 Subject: [PATCH] 最新版本541200007 --- server/visits/dmvisit_admin/src/main/java/com/doumee/cloud/common/PublicCloudController.java | 515 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 515 insertions(+), 0 deletions(-) diff --git a/server/visits/dmvisit_admin/src/main/java/com/doumee/cloud/common/PublicCloudController.java b/server/visits/dmvisit_admin/src/main/java/com/doumee/cloud/common/PublicCloudController.java index e69de29..831575c 100644 --- a/server/visits/dmvisit_admin/src/main/java/com/doumee/cloud/common/PublicCloudController.java +++ b/server/visits/dmvisit_admin/src/main/java/com/doumee/cloud/common/PublicCloudController.java @@ -0,0 +1,515 @@ +package com.doumee.cloud.common; + +import cn.hutool.core.io.IoUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.doumee.api.BaseController; +import com.doumee.biz.system.SystemDictDataBiz; +import com.doumee.core.annotation.trace.Trace; +import com.doumee.core.constants.ResponseStatus; +import com.doumee.core.exception.BusinessException; +import com.doumee.core.haikang.model.HKConstants; +import com.doumee.core.haikang.model.param.BaseResponse; +import com.doumee.core.haikang.model.param.request.FacePictureCheckRequest; +import com.doumee.core.haikang.model.param.respose.FacePictureCheckResponse; +import com.doumee.core.haikang.service.HKService; +import com.doumee.core.utils.Constants; +import com.doumee.core.utils.DateUtil; +import com.doumee.core.utils.FtpUtil; +import com.doumee.core.utils.HttpsUtil; +import com.doumee.core.utils.aliyun.ALiYunUtil; +import com.doumee.dao.business.dto.DownloadFileDTO; +import com.doumee.dao.system.model.SystemDict; +import com.doumee.dao.system.model.SystemDictData; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.compress.archivers.zip.Zip64Mode; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +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; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.util.*; + +/** + * @author Eva.Caesar Liu + * @date 2023/02/14 11:14 + */ +@Api(tags = "鍏叡涓婁紶鎺ュ彛") +@Trace(exclude = true) +@RestController +@RequestMapping(Constants.CLOUD_SERVICE_URL_INDEX+"/public") +@Slf4j +public class PublicCloudController extends BaseController { + @Autowired + private SystemDictDataBiz systemDictDataBiz; + + + + public static FtpUtil ftp = null; + + @ApiOperation(value = "鎵归噺涓婁紶鏂囦欢鍒癋TP") + @RequestMapping(method= RequestMethod.POST,value="/uploadBatch") + @ResponseBody + public void uploadBatch(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 { + 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(); + } + + 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; + } + + } + +// 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) { + 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 = "涓嬭浇缃戠粶鏂囦欢") + @RequestMapping(method= RequestMethod.POST,value="/downloadFile") + @ResponseBody + public void downloadFile(@RequestBody DownloadFileDTO param, HttpServletRequest request, HttpServletResponse response) throws Exception { + if(StringUtils.isBlank(param.getUrl())){ + throw new BusinessException(ResponseStatus.BAD_REQUEST); + } + InputStream inputStream = HttpsUtil.connectionInputsteam(param.getUrl(),"GET",null,null); + if(inputStream == null){ + throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"鏂囦欢涓嬭浇澶辫触锛岃妫�鏌ユ枃浠惰矾寰勬槸鍚﹀悎娉曪紒"); + } + if(StringUtils.isBlank(param.getFileName())){ + param.setFileName(System.currentTimeMillis()+""); + if(param.getUrl().lastIndexOf(".") >0){ + String nfix = param.getUrl().substring(param.getUrl().lastIndexOf(".")); + param.setFileName(System.currentTimeMillis()+"."+nfix); + } + } + + String encodeFileName = URLEncoder.encode(param.getFileName(), Charset.forName("UTF-8").toString()); + response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName); + response.setContentType("application/octet-stream"); + response.setHeader("eva-opera-type", "download"); + response.setHeader("eva-download-filename", encodeFileName); + + OutputStream os = new BufferedOutputStream(response.getOutputStream()); + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + os.write(buffer, 0, bytesRead); + } + os.flush(); + os.close(); + inputStream.close(); + } + @ApiOperation(value = "涓婁紶鏂囦欢鍒癋TP") + @RequestMapping(method= RequestMethod.POST,value="/upload") + @ResponseBody + public void upload(HttpServletRequest request, HttpServletResponse response, String folder,Integer isFace) throws Exception { +// folder = systemDictDataBiz.queryByCode(Constants.FTP,folder).getCode(); + Date d1 = new Date(); + log.error("鎬诲緱涓婁紶鏂囦欢鎴愬姛=============寮�濮�========="+DateUtil.getPlusTime2(d1)); + folder =StringUtils.defaultString(folder).replace("/", ""); + String prefixPath = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode(); + InputStream is = null; + response.setCharacterEncoding("UTF-8"); + 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(); + } + + CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); + if (multipartResovler.isMultipart(request)) { + MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; + Iterator<String> it = multipartRequest.getFileNames(); + while (it.hasNext()) { + MultipartFile file = multipartRequest.getFile(it.next()); + String originname = file.getOriginalFilename(); + is = file.getInputStream(); + String endType = ".jpg"; + if(originname.indexOf(".")>0){ + 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){ + if(Objects.nonNull(isFace) && Constants.equalsInteger(isFace,Constants.ZERO)){ + //楠岃瘉浜鸿劯璇勫垎 + //浜鸿劯璇勫垎 + FacePictureCheckRequest param = new FacePictureCheckRequest(); + String prefixUrl = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_LOCAL_RESOURCE_PATH).getCode(); + param.setFacePicUrl(prefixUrl + fileName); + BaseResponse<FacePictureCheckResponse> responseBaseResponse = HKService.facePictureCheck(param); + if(responseBaseResponse == null || !StringUtils.equals(responseBaseResponse.getCode(), HKConstants.RESPONSE_SUCCEE)){ + context.put("code", 0); + context.put("message", "瀵逛笉璧凤紝浜鸿劯璇勫垎鑾峰彇鏁版嵁澶辫触~"); + context.put("errno",0); + writerJson(response, context); + return; + } + FacePictureCheckResponse facePictureCheckResponse = responseBaseResponse.getData(); + if(Objects.isNull(facePictureCheckResponse) || Objects.isNull(facePictureCheckResponse.getCheckResult())){ + context.put("code", 0); + context.put("message", "瀵逛笉璧凤紝浜鸿劯璇勫垎鑾峰彇鏁版嵁澶辫触~"); + context.put("errno",0); + writerJson(response, context); + return; + } + if(!facePictureCheckResponse.getCheckResult()){ + context.put("code", 0); + context.put("message", facePictureCheckResponse.getStatusMessage()); + context.put("errno",0); + writerJson(response, context); + return; + } + + } + + context.put("success", true); + context.put("code", 200); + context.put("errno",0); + JSONObject fileJSON = new JSONObject(); +// fileJSON.put("prefixPath", prefixPath); +// fileJSON.put("folder", folder); + fileJSON.put("url", prefixPath+fileName); + fileJSON.put("imgaddr", fName); + fileJSON.put("fileSize", file.getSize()); + fileJSON.put("imgname", fileName); + fileJSON.put("originname", originname); + context.put("data",fileJSON); + context.put("message","璇锋眰鎴愬姛"); + writerJson(response, context); + Date d2= new Date(); + log.error("鎬荤殑涓婁紶鏂囦欢鎴愬姛=============缁撴潫========="+DateUtil.getPlusTime2(d2) +"鑰楁椂绉掞細"+( (d2.getTime()-d1.getTime()) /1000)); + return; + } + } + } + } catch (Exception e) { + log.error("銆愪笂浼燜TP澶辫触銆�======================"+e.getMessage()); + } + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + context.put("errno",0); + writerJson(response, context); + return; + } + + public void upload(HttpServletRequest request, HttpServletResponse response, String folder, String bucketName, + String access_id, String access_key, String resourcePath, String endpoint) throws Exception { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=UTF-8"); + Map<String, Object> context = new HashMap<>(); + CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); + if (multipartResovler.isMultipart(request)) { + MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; + Iterator<String> it = multipartRequest.getFileNames(); + while (it.hasNext()) { + MultipartFile file = multipartRequest.getFile((String) it.next());// file + // multipartRequest.getFile((String) + // it.next()); + if (file != null) { + // 1銆佷笂浼犲埌鏈嶅姟鍣ㄤ复鏃舵枃浠跺す + String uploadFileName = file.getOriginalFilename(); + String originname = uploadFileName; + if (originname.lastIndexOf("/") >= 0) { + originname = originname.substring(originname.lastIndexOf("/") + 1); + } + String nfix = "";// 鍚庣紑鍚� + if (StringUtils.isNotBlank(uploadFileName)) { + nfix = uploadFileName.substring(uploadFileName.lastIndexOf(".")); + } + if (StringUtils.equalsIgnoreCase(nfix, ".exe")) { + context.put("code", 4000); + context.put("message", "瀵逛笉璧凤紝鏂囦欢鏍煎紡\".exe\"涓婁紶鏈夎锛�"); + return; + } + if (StringUtils.equalsIgnoreCase(nfix, ".dll")) { + context.put("code", 4000); + context.put("message", "瀵逛笉璧凤紝鏂囦欢鏍煎紡\".dll\"涓婁紶鏈夎锛�"); + return; + } + String nowDate = DateUtil.getNowShortDate();// 褰撳墠鏃堕棿锛堝勾鏈堟棩锛� + String fileName = UUID.randomUUID().toString() + nfix; + String tempFileName = nowDate + "/" + fileName; + String key = folder + tempFileName;// 鏂囦欢鍚� + ALiYunUtil obs = new ALiYunUtil(endpoint,access_id, access_key); + if (obs.uploadOnlineObject(file.getInputStream(),bucketName, key,null)) { + // 绉诲姩鎴愬姛,杩斿洖鏂囦欢鍚� + // sendSuccessMessage(response, resourcePath+key); + context.put("success", true); + context.put("code", 200); + context.put("errno",0); + JSONObject fileJSON = new JSONObject(); + fileJSON.put("url", resourcePath + key); + fileJSON.put("imgaddr", tempFileName); + fileJSON.put("imgname", fileName); + fileJSON.put("originname", originname); + context.put("data",fileJSON); + context.put("message","璇锋眰鎴愬姛"); + writerJson(response, context); + return; + } else { + // 绉诲姩澶辫触 + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + writerJson(response, context); + return; + } + } + + } + } + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + context.put("errno",0); + writerJson(response, context); + return; + } + + public static void writerJson(HttpServletResponse response, Object object) { + response.setContentType("application/json"); + writer(response, JSONObject.toJSONString(object)); + } + + private static void writer(HttpServletResponse response, String str) { + try { + StringBuffer result = new StringBuffer(); + //璁剧疆椤甸潰涓嶇紦瀛� + response.setHeader("Pragma", "No-cache"); + response.setHeader("Cache-Control", "no-cache"); + response.setCharacterEncoding("UTF-8"); + PrintWriter out = null; + out = response.getWriter(); + out.print(str); + out.flush(); + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void uploadFileLocal(HttpServletRequest request, String folder, HttpServletResponse response, String rootPath,String dir) throws Exception { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=UTF-8"); + CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver(); + Map<String, Object> context = new HashMap<>(); + if (multipartResovler.isMultipart(request)) { + MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; + Iterator<String> it = multipartRequest.getFileNames(); + while (it.hasNext()) { + MultipartFile file = multipartRequest.getFile((String) it.next());// file + // = + // (CommonsMultipartFile) + // multipartRequest.getFile((String) + // it.next()); + if (file != null) { + if (file.getSize() > 200 * 1024 * 1024L) { + context.put("code", 4000); + context.put("message", "涓婁紶鏂囦欢杩囧ぇ"); + return; + } + System.out.println(file.getOriginalFilename()); + if (file.getOriginalFilename() == null) { + context.put("code", 4000); + context.put("message", "鏂囦欢鍚嶄笉鍙负绌�"); + return; + } + /* + * if(file.getOriginalFilename().contains(",")||file.getOriginalFilename(). + * contains(" ")){ sendFailureMessage(response,"鏂囦欢鍚嶇О鏈夎,涓嶅彲鍚湁閫楀彿绛夌壒娈婂瓧绗�"); } + */ + + String nowDate = DateUtil.getNowShortDate(); + folder += nowDate + "/"; + String strDirPath = rootPath + folder; + File dirPath = new File(strDirPath); + if (!dirPath.exists()) { + dirPath.mkdirs(); + } + String uploadFileName = file.getOriginalFilename(); + String x = UUID.randomUUID().toString().replace("-", "") + + uploadFileName.substring(uploadFileName.lastIndexOf(".")); + String fileName = folder + x; + String fileNames = nowDate + "/" + x; + uploadFileName = uploadFileName.replace(" ", ""); + uploadFileName = uploadFileName.replace("锛�", ","); + uploadFileName = uploadFileName.replaceAll(",", "-"); + System.err.println("R:" + fileName); + String fileAndPath = dir + fileName; + System.err.println("A:" + fileAndPath); + // 鍒ゆ柇濡傛灉涓存椂鐩綍涓瓨鍦ㄧ浉鍚屽悕绉扮殑鏂囦欢鍏堝垹闄わ紝鍦ㄤ笂浼� + File tempFile = new File(rootPath + fileName); + if (tempFile.isFile() && tempFile.exists()) { + tempFile.getAbsoluteFile().delete(); + } + // 涓婁紶鍒版湇鍔″櫒涓存椂鏂囦欢澶� + file.transferTo(tempFile); + // 杞Щ鍒癋TP鏈嶅姟鍣� + String nfix = ""; + if (null != uploadFileName) { + nfix = uploadFileName.substring(uploadFileName.lastIndexOf(".")); + } + + if (StringUtils.equalsIgnoreCase(nfix, ".exe")) { + context.put("code", 4000); + context.put("message", "瀵逛笉璧凤紝鏂囦欢鏍煎紡\".exe\"涓婁紶鏈夎锛�"); + return; + } + if (StringUtils.equalsIgnoreCase(nfix, ".dll")) { + context.put("code", 4000); + context.put("message", "瀵逛笉璧凤紝鏂囦欢鏍煎紡\".dll\"涓婁紶鏈夎锛�"); + return; + } + + String remoteName = fileName; + String remoteFileName = fileAndPath; + + /* Map map = new HashMap(); + map.put("url", remoteFileName); + map.put("alt", uploadFileName); + map.put("href", remoteFileName); + List maps = Arrays.asList(remoteFileName);*/ + + if (true) { + // 鍏抽棴FTP娴� + // 绉诲姩鎴愬姛,杩斿洖鏂囦欢鍚� + + /*context.put("error", 0); + context.put("url", remoteFileName); + context.put("fullurl", remoteFileName); + context.put("fname", uploadFileName); + context.put("data", maps); + context.put("halfurl", fileNames);*/ + + context.put("success", true); + context.put("code", 200); + context.put("errno",0); + JSONObject fileJSON = new JSONObject(); + fileJSON.put("url", remoteFileName); + fileJSON.put("imgaddr", fileNames); + fileJSON.put("imgname", uploadFileName); + fileJSON.put("originname", uploadFileName); + context.put("data",fileJSON); + context.put("message","璇锋眰鎴愬姛"); + + writerJson(response, context); + return; + } else { + // 绉诲姩澶辫触 + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + writerJson(response, context); + } + } + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + writerJson(response, context); + return; + } + } + context.put("code", 0); + context.put("message", "涓婁紶澶辫触"); + writerJson(response, context); + return; + } +} -- Gitblit v1.9.3