JAVA下载图片压缩zip
Posted 美宰可
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA下载图片压缩zip相关的知识,希望对你有一定的参考价值。
1. 支持多张图片下载
/**
* 下载附件zip
*/
@PostMapping("downloadZip")
public void downloadZip(@RequestBody ShipmentAnnexVo shipmentAnnexVo, HttpServletRequest request, HttpServletResponse response)
shipmentAnnexService.downloadZip(shipmentAnnexVo, request, response);
2. serviceImpl实现逻辑
// zip文件名
String zipName = DateUtil.getNowDateYMDHMS();
try
// 创建zip输出流,输出到客户端
ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
zipName = URLEncoder.encode(zipName, "UTF-8");
response.reset();
response.setCharacterEncoding("UTF-8");
// 不同类型的文件对应不同的MIME类型
response.setContentType("application/octet-stream");
// inline在浏览器中直接显示,不提示用户下载
// attachment弹出对话框,提示用户进行下载保存本地
// 默认为inline方式
response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
for (ShipmentAnnex annex : annexList)
InputStream in = tencentMinioManage.get(bucketName, annex.getAnnexFileName());
String fileName = annex.getOriginFileName().contains(".") ? annex.getOriginFileName() : annex.getOriginFileName() + ".jpg";
out.putNextEntry(new ZipEntry(fileName));
int len;
byte[] buf = new byte[4096];
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
out.closeEntry();
in.close();
out.close();
catch (Exception e)
throw new BusinessException("下载ZIP异常!" + e.getMessage());
Java 将文件,文件夹打入压缩包zip,rar
批量下载经常会有将文件打包下载的需求,比如批量下载图片
今天就分享一个java将文件夹中的所有文件(包括文件夹,递归)打包成压缩包(支持rar和zip)
先看效果,测试目录
李四文件夹下有文件夹和,文件和一个空文件夹(空文件夹打包到压缩包显示图标和类型都有点问题,这个还没找的解决方案,可以不打进去
)
压缩包截图(打包空目录)
不打包空目录(rar)
依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtil
public static void main(String[] args) throws Exception
String dir = "E:\\\\zip";
String zip = "E:\\\\target.zip";
String rar = "E:\\\\target.rar";
zip(dir, zip);
zip(dir, rar, true);
/**
* 打包
*
* @param dir 要打包的目录
* @param zipFile 打包后的文件路径
* @throws Exception
*/
public static void zip(String dir, String zipFile) throws Exception
zip(dir, zipFile, false);
/**
* 打包
*
* @param dir 要打包的目录
* @param zipFile 打包后的文件路径
* @param includeBaseDir 是否包括最外层目录
* @throws Exception
*/
public static void zip(String dir, String zipFile, boolean includeBaseDir) throws Exception
if (zipFile.startsWith(dir))
throw new RuntimeException("打包生成的文件不能在打包目录中");
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)))
File fileDir = new File(dir);
String baseDir = "";
if (includeBaseDir)
baseDir = fileDir.getName();
compress(out, fileDir, baseDir);
public static void compress(ZipOutputStream out, File sourceFile, String base) throws Exception
if (sourceFile.isDirectory())
base = base.length() == 0 ? "" : base + File.separator;
File[] files = sourceFile.listFiles();
if (ArrayUtils.isEmpty(files))
// todo 打包空目录
// out.putNextEntry(new ZipEntry(base));
return;
for (File file : files)
compress(out, file, base + file.getName());
else
out.putNextEntry(new ZipEntry(base));
try (FileInputStream in = new FileInputStream(sourceFile))
IOUtils.copy(in, out);
catch (Exception e)
throw new RuntimeException("打包异常: " + e.getMessage());
参考文献
https://www.cnblogs.com/tobiasy/p/8258419.html
以上是关于JAVA下载图片压缩zip的主要内容,如果未能解决你的问题,请参考以下文章