From e356b905490c0a993f638d55859e23ce336bda82 Mon Sep 17 00:00:00 2001 From: "pref_mail@163.com" <pref_mail@163.com> Date: Mon, 4 Dec 2023 14:55:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springblade/common/utils/FileUtil.java | 73 +++++++++++-------- .../impl/DistributionSignforServiceImpl.java | 21 +++--- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/blade-biz-common/src/main/java/org/springblade/common/utils/FileUtil.java b/blade-biz-common/src/main/java/org/springblade/common/utils/FileUtil.java index f296b8dc3..dbbac93ae 100644 --- a/blade-biz-common/src/main/java/org/springblade/common/utils/FileUtil.java +++ b/blade-biz-common/src/main/java/org/springblade/common/utils/FileUtil.java @@ -1,6 +1,8 @@ package org.springblade.common.utils; +import lombok.extern.log4j.Log4j2; import org.springblade.common.constant.CommonConstant; +import org.springblade.common.exception.CustomerException; import javax.servlet.http.HttpServletResponse; import java.io.*; @@ -20,6 +22,7 @@ import static io.undertow.servlet.core.BlockingWriterSenderImpl.BUFFER_SIZE; * @author cyz * @since 2023-05-16 */ +@Log4j2 public class FileUtil extends TimerTask { private List<String> LocalTempFiles; @@ -28,10 +31,7 @@ public class FileUtil extends TimerTask { //loadhost // private static String savePath = "D:/"+"reservationZip/"; - private static String zipPavePath = CommonConstant.SYSTEMFILEPATH+"reservationZip/"; - - - + private static String zipPavePath = CommonConstant.SYSTEMFILEPATH + "reservationZip/"; public FileUtil(List<String> list) { @@ -39,29 +39,40 @@ public class FileUtil extends TimerTask { } - /** * 对指定的文件进行压缩,并返回文件名称 + * * @param reservationCodes * @return */ - public static String reservationPictureFileToZip(String reservationCodes) { - String currentTimeMillis = System.currentTimeMillis()+".zip"; - try{ + public static String reservationPictureFileToZip(String reservationCodes) { + String currentTimeMillis = System.currentTimeMillis() + ".zip"; + FileOutputStream fos = null; + ZipOutputStream zos = null; + try { //zip文件生成位置 - File zipFile = new File(zipPavePath+"/"+ currentTimeMillis); - FileOutputStream fos = new FileOutputStream(zipFile); - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos)); + File zipFile = new File(zipPavePath + "/" + currentTimeMillis); + fos = new FileOutputStream(zipFile); + zos = new ZipOutputStream(new BufferedOutputStream(fos)); for (String s : reservationCodes.split(",")) { - File file = new File(zipPavePath+s); - if (!Objects.isNull(file)){ - fileToZip(zos, file, ""); + File file = new File(zipPavePath + s); + fileToZip(zos, file, ""); + } + + } catch (Exception e) { + throw new CustomerException(500,"压缩文件失败!"); + } finally { + try { + if (zos != null) { + zos.close(); + } + if (fos != null) { + fos.close(); } + } catch (Exception e) { + log.error("压缩文件关闭流报错 >> {}",e.getMessage()); } - zos.close(); - fos.close(); - }catch (Exception e){ - throw new RuntimeException(e); + } return currentTimeMillis; @@ -69,7 +80,7 @@ public class FileUtil extends TimerTask { public static File getZipFile(String fileName) { - return new File(zipPavePath +fileName); + return new File(zipPavePath + fileName); } @@ -94,7 +105,7 @@ public class FileUtil extends TimerTask { */ public static void handleSignPictureZip(List<String> urls, String type, String code, String consignee) { //将图片下载至本地中 - String imgPath = zipPavePath+code+"/"; + String imgPath = zipPavePath + code + "/"; // 打开连接 //设置请求超时为20s @@ -108,55 +119,55 @@ public class FileUtil extends TimerTask { //堆码 break; case "photo_3": - imgPath +="/friability"; + imgPath += "/friability"; //易碎 break; case "photo_4": - imgPath +="/home-delivery"; + imgPath += "/home-delivery"; //家配 break; case "photo_5": - imgPath +="/signing"; + imgPath += "/signing"; //签收 break; } for (String imgUrl : urls) { - String str = imgUrl.substring(imgUrl.lastIndexOf("/")+1, imgUrl.length()); + String str = imgUrl.substring(imgUrl.lastIndexOf("/") + 1, imgUrl.length()); try { //下载远程地址图片 download(imgUrl, imgPath, str); //将该客户的签收图片进行ZIP压缩 - } catch (Exception e) { + } catch (Exception e) { throw new RuntimeException(e); } } - } - private static void fileToZip(ZipOutputStream zos, File sourceFile, String path) throws Exception { //如果是文件夹只创建zip实体即可,如果是文件,创建zip实体后还要读取文件内容并写入 if (sourceFile.isDirectory()) { path = path + sourceFile.getName() + "/"; ZipEntry zipEntry = new ZipEntry(path); zos.putNextEntry(zipEntry); - for (File file : sourceFile.listFiles()) { + for (File file : Objects.requireNonNull(sourceFile.listFiles())) { fileToZip(zos, file, path); } } else { //创建ZIP实体,并添加进压缩包 ZipEntry zipEntry = new ZipEntry(path + sourceFile.getName()); zos.putNextEntry(zipEntry); - byte[] bufs = new byte[1024 * 10]; + + int bufsNum =400; + byte[] bufs = new byte[bufsNum]; //读取待压缩的文件并写进压缩包里 FileInputStream fis = new FileInputStream(sourceFile); - BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10); + BufferedInputStream bis = new BufferedInputStream(fis, bufsNum); int read = 0; - while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) { + while ((read = bis.read(bufs, 0, bufsNum)) != -1) { zos.write(bufs, 0, read); } bis.close(); diff --git a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSignforServiceImpl.java b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSignforServiceImpl.java index 5fe01845e..88496c883 100644 --- a/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSignforServiceImpl.java +++ b/blade-service/logpm-distribution/src/main/java/com/logpm/distribution/service/impl/DistributionSignforServiceImpl.java @@ -3041,26 +3041,26 @@ public class DistributionSignforServiceImpl extends BaseServiceImpl<Distribution if (Func.isNotEmpty(distributionSignforEntities)) { for (DistributionSignforEntity distributionSignforEntity : distributionSignforEntities) { - log.info(">>>>>> handlePicture {}",3-1); + log.info(">>>>>> handlePicture {},{}",3,1); DistributionReservationEntity distributionReservationEntity = distributionReservationMapper.selectById(distributionSignforEntity.getReservationId()); - log.info(">>>>>> handlePicture {}",3-2); + log.info(">>>>>> handlePicture {},{}",3,2); List<DistributionSignPrintVO> printEntities = baseMapper.selectSignImgsUrl(distributionSignforEntity.getReservationId()); - log.info(">>>>>> handlePicture {}",3-3); + log.info(">>>>>> handlePicture {},{}",3,3); if (Func.isNotEmpty(printEntities)){ //进行分组 Map<String, List<DistributionSignPrintVO>> signingPictureGroup = printEntities.stream().collect(Collectors.groupingBy(DistributionSignPrintVO::getType)); - log.info(">>>>>> handlePicture {}",3-4); + log.info(">>>>>> handlePicture {},{}",3,4); //进行图片 signingPictureGroup.forEach((k,v)->{ - log.info(">>>>>> handlePicture {}",3-5); + log.info(">>>>>> handlePicture {},{}",3,5); //这个方法会根据 List<String> urls = v.stream().map(DistributionSignPrintVO::getUrlRoute).collect(Collectors.toList()); - log.info(">>>>>> handlePicture {}",3-6); + log.info(">>>>>> handlePicture {},{}",3,6); FileUtil.handleSignPictureZip(urls, k, distributionReservationEntity.getReservationCode(), distributionReservationEntity.getConsignee()); - log.info(">>>>>> handlePicture {}",3-7); + log.info(">>>>>> handlePicture {},{}",3,7); }); //拼接ReservationCode @@ -3077,8 +3077,10 @@ public class DistributionSignforServiceImpl extends BaseServiceImpl<Distribution //获得压缩后的文件进行前端下载数据推送 File file = FileUtil.getZipFile(fileName); + + log.info(">>>>>> handlePicture {}",7); if (Func.isNotEmpty(file)){ - byte[] buffer = new byte[1024 * 1024 * 1024]; + byte[] buffer = new byte[512]; FileInputStream fis = null; BufferedInputStream bis = null; try { @@ -3094,8 +3096,7 @@ public class DistributionSignforServiceImpl extends BaseServiceImpl<Distribution i = bis.read(buffer); } } catch (IOException e) { - log.info("Download failed!"); - throw new RuntimeException(e); + log.error("文件下载失败 {}",e.getMessage()); }finally { if (bis != null) { try {