參考以下博主:文章來源:http://www.zghlxwxcb.cn/news/detail-759255.html
Java實(shí)現(xiàn)文件下載zip包單文件等_java下載zip文件_liu.kai的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-759255.html
1、核心步驟
- 先將需要壓縮的文件們打包在一塊生成一個臨時壓縮包
- 將這個臨時的壓縮包,以單文件下載的方式,給前端響應(yīng)過去
- 刪除臨時的壓縮包
2、代碼
package mrkay.show.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 自定義 (zip壓縮包/文件) 操作工具類
*
* @author liukai
* @date 2022/7/14
*/
@Slf4j
@SuppressWarnings("all")
public class ZipFileUtils {
/**
* 生成Zip壓縮包 (注意是對hutool的二次封裝,所以必須要有hutool依賴)
*
* @param targetZipFile 要生成的目標(biāo)zip壓縮包
* @param sourceFiles 壓縮包中包含的文件集合
* @param dirWithFlag 是否將文件目錄一同打包進(jìn)去 (true:壓縮包中包含文件目錄,false:壓縮包中不包含目錄)
* @author liukai
*/
public static void generateZip(File targetZipFile, List<File> sourceFiles, boolean dirWithFlag) {
if (CollUtil.isNotEmpty(sourceFiles)) {
File[] fileArr = sourceFiles.toArray(new File[]{});
ZipUtil.zip(targetZipFile, dirWithFlag, fileArr);
}
}
/**
* 下載ZIP壓縮包(會對下載后的壓縮包進(jìn)行刪除)
*
* @param file zip壓縮包文件
* @param response 響應(yīng)
* @author liukai
*/
public static void downloadZip(File file, HttpServletResponse response) {
OutputStream toClient = null;
try {
// 以流的形式下載文件。
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
log.error("下載zip壓縮包過程發(fā)生異常:", e);
} finally {
if (toClient != null) {
try {
toClient.close();
} catch (IOException e) {
log.error("zip包下載關(guān)流失敗:", e);
}
}
//刪除改臨時zip包(此zip包任何時候都不需要保留,因?yàn)樵次募S時可以再次進(jìn)行壓縮生成zip包)
file.delete();
}
}
/**
* 任何單文件下載
*
* @param file 要下載的文件
* @param response 響應(yīng)
* @author liukai
*/
public static void downloadAnyFile(File file, HttpServletResponse response) {
FileInputStream fileInputStream = null;
OutputStream outputStream = null;
try {
fileInputStream = new FileInputStream(file);
// 清空response
response.reset();
//防止文件名中文亂碼
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(),"UTF-8"));
//根據(jù)文件動態(tài)setContentType
response.setContentType(new MimetypesFileTypeMap().getContentType(file) + ";charset=UTF-8");
outputStream = response.getOutputStream();
byte[] bytes = new byte[2048];
int len;
while ((len = fileInputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
到了這里,關(guān)于使用hutool工具,對多文件下載進(jìn)行打包下載,這里使用的是zip壓縮算法。的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!