package pdf;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
public class ZipDownFile {
public static List<Map<String, Object>> getRomteFileData(List<String> httpUrls) throws IOException {
String strTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
List<Map<String, Object>> files = new ArrayList<>();
Set<String> existFileName = new HashSet<>();
for (String httpUrl : httpUrls) {
String fileName = null;
Map<String, Object> map = new HashMap<>();
InputStream inputStream = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
URL url = new URL(httpUrl);
String fileNameEncode = org.apache.commons.lang3.StringUtils.substringAfter(url.getFile(), "&fileName=");
fileName = URLDecoder.decode(fileNameEncode, CharsetUtil.UTF_8);
if (!existFileName.add(fileName)) {
continue;
}
map.put("fileName", strTime + fileName + ".pdf");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(10 * 1000);
inputStream = con.getInputStream();
if (Objects.nonNull(inputStream)) {
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
bos.write(buf, 0, len);
}
}
} catch (Exception e) {
bos.write((fileName + e.getMessage()).getBytes(StandardCharsets.UTF_8));
map.put("fileName", strTime + fileName + ".txt");
log.error("获取远程文件信息错误信息:" + e.getMessage());
} finally {
IoUtil.close(inputStream);
IoUtil.close(bos);
}
map.put("outByte", bos.toByteArray());
files.add(map);
}
return files;
}
public static void downloadZip(HttpServletResponse response, List<Map<String, Object>> maps) throws IOException {
String strTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(strTime + "对账单", "UTF-8") + ".zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
try {
for (Map<String, Object> map : maps) {
ZipEntry zipEntry = new ZipEntry((String) map.get("fileName"));
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write((byte[]) map.get("outByte"));
}
} catch (Exception e) {
log.error("下载远程文件信息错误信息:" + e.getMessage());
} finally {
IoUtil.close(zipOutputStream);
}
}
public static void downloadZip(OutputStream outputStream, List<Map<String, Object>> maps) throws IOException {
String strTime = DateUtil.format(new Date(), "yyyyMMddHHmmss");
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
try {
for (Map<String, Object> map : maps) {
ZipEntry zipEntry = new ZipEntry((String) map.get("fileName"));
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write((byte[]) map.get("outByte"));
}
} catch (Exception e) {
log.error("下载远程文件信息错误信息:" + e.getMessage());
} finally {
IoUtil.close(zipOutputStream);
}
}
}
@Test
public void test02() throws Exception {
List<String> httpUrls = new ArrayList<>();
httpUrls.add("https://tsign.49icloud.com/rest/file-system/operation/download?fileName=对账单1");
httpUrls.add("https://tsign.49icloud.com/rest/file-system/operation/download?fileName=对账单2");
List<Map<String, Object>> romteFileData = ZipDownFile.getRomteFileData(httpUrls);
String targetFile = "D:\\reloadD\\www\\java\\study\\_2020\\src\\main\\java\\pdf\\downFile.zip";
ZipDownFile.downloadZip(new FileOutputStream(targetFile), romteFileData);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?