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;
/**
* @date 2022/10/30
**/
@Slf4j
public class ZipDownFile {
/***
* 获取远程文件信息
* @param httpUrls
* @return {@link List< Map< String, Object>>}
* @date 2022/10/30
**/
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;
}
/***
* 批量下载文件
* @param response
* @param maps
* @date 2022/10/30
**/
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);
}
}
/***
* 批量保存文件
* @param outputStream
* @param maps
* @date 2022/10/30
**/
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);
}
标签:map,java,String,批量,import,fileName,new,PDF
From: https://www.cnblogs.com/fuqian/p/16842289.html