一、使用hutool 工具类
效果:下载速度可以
1、依赖:hutool
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version>
</dependency>
2、调用方式
import cn.hutool.core.util.*;
HttpServletResponse response;
List<String> resourceList = StrUtil.split(fileUrls, ",");
List<File> fileList = resourceList.stream()
.map(x->new File("文件路径" + 根据预览路径获取对应文件路径(x)))
.toList();
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, zipName);
ZipUtil.zip(response.getOutputStream(), Charset.defaultCharset(),true,null, ArrayUtil.toArray(fileList,File.class));
3、依赖工具类
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
public class FileUtils {
/**
* 百分号编码工具方法
*
* @param s 需要百分号编码的字符串
* @return 百分号编码后的字符串
*/
public static String percentEncode(String s) {
return URLUtil.encode(s).replaceAll("\\+", "%20");
}
/**
* 下载文件名重新编码
*
* @param response 响应对象
* @param realFileName 真实文件名
*/
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) {
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(percentEncodedFileName)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(percentEncodedFileName);
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
response.setHeader("Content-disposition", contentDispositionValue.toString());
response.setHeader("download-filename", percentEncodedFileName);
}
}
二、原生java方式
参考
使用ZipEntry压缩与解压缩-CSDN博客文章浏览阅读1.8w次,点赞4次,收藏31次。import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io_zipentryhttps://blog.csdn.net/sinat_34620530/article/details/54093862
java下载zip附件_zipentry通过链接下载附件-CSDN博客文章浏览阅读1.1k次。1. 将文件下载到文件夹中,打包zip放入浏览器/* * Copyright 2019 yifen7.com All right reserved. This software is the * confidential and proprietary information of yifen7.com ("Confidential * Information"). ..._zipentry通过链接下载附件https://blog.csdn.net/weixin_41564440/article/details/102631447
1、原生的下载
可以封装下载工具类,这里就没优雅实现(懒得改之前的代码)
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
HttpServletResponse response = 前台获取;
String fileName = "data_bk_" + getCurrentTimeStr();
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".zip");
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream= null;
ry {
outputStream= response.getOutputStream();
// 2 新建压缩包、文件扩展名根据驱动判断
ZipOutputStream zos = new ZipOutputStream(outputStream);
String saveSqlName = "添加的文件名";
// 1.1 将备份的sql放入压缩包
ZipEntry fileEntry = new ZipEntry(saveSqlName);
zos.putNextEntry(fileEntry);
// 1.2 通过文件流将该文件写入
String saveSqlFile = "d:\\xx.sql"
FileInputStream fis = new FileInputStream(saveSqlFile);
byte[] buffer = new byte[4096];
int len;
while ((len = fis.read(buffer)) != -1) {
messageDigest.update(buffer, 0, len);
zos.write(buffer, 0, len);
zos.flush();
}
fis.close();
zos.closeEntry();
//或者
/**
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
while ((count = bis.read(data, 0, bufferLen)) != -1) {
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
**/
//2.1 多个文件重复操作...
} catch (Exception e) {
log.error("数据备份出错:{}", e);
} finally {
try {
os.flush();
os.close();
} catch (IOException e) {
log.error("数据备份出错:{}", e);
}
}
版权归原作者 小云小白 所有, 如有侵权,请联系我们删除。