public static void processImagesToZip(HttpServletResponse response, List<String> ossList) throws IOException {
// 将 OSS 图片地址转换成图片文件
List<File> fileList = new ArrayList<>();
for (String ossUrl : ossList) {
try {
BufferedImage image = ImageIO.read(new URL(ossUrl));
//
String fileName = ossUrl.substring(ossUrl.lastIndexOf("/") + 1)+ UUID.randomUUID()+".jpg";
File file = new File(fileName);
ImageIO.write(image, "jpg", file);
fileList.add(file);
} catch (IOException e) {
e.printStackTrace();
}
}
// 打包压缩图片文件
File zipFile = new File("kouqiang.zip");
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
byte[] buffer = new byte[1024];
for (File file : fileList) {
if (file.exists()) {
ZipEntry zipEntry = new ZipEntry(file.getName());
zipOut.putNextEntry(zipEntry);
将图片链接转换成图片打成压缩包的工具类(有直接打包, 有分文件夹打包的)
最新推荐文章于 2024-09-16 20:08:29 发布
该博客介绍了一个Java工具类,用于将包含图片链接的集合转换为压缩包,支持直接打包和按文件夹打包两种方式。内容详细解析了如何实现这一功能,最终生成的压缩包结构直观易用。
摘要由CSDN通过智能技术生成