|
|
|
@ -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(); |
|
|
|
|