通过url地址批量打包zip下载文件
controller
@ApiOperation("通过下载url批量打包zip下载")
@PostMapping("batchDownloadFile")
public void batchDownloadFile(@RequestBody List<String> urlList){
log.debug("通过下载url批量打包zip下载 入参 {} ", urlList.toString());
if (CollectionUtils.isEmpty(attachmentVoList)){
return;
}
fileService.batchDownloadFile(urlList,this.getResponse(),this.getRequest());
}
service接口
/**
* 通过下载url批量打包zip下载
* @param urlList需下载的文件信息(下载地址,文件名称)
* @param response 响应
*/
void batchDownloadFile(List<String> urlList, HttpServletResponse response, HttpServletRequest request);
service实现类
/** 需要压缩的文件夹 */
private final static String downloadDir = "download";
/** 打包后的文件夹 */
private final static String downloadZip = "downloadZip";
/**
* 重载通过下载url批量打包zip下载
* @param urlList需下载的文件信息(下载地址,文件名称)
* @param response 响应
*/
@Override
public void batchDownloadFile(List<String> urlList, HttpServletResponse response, HttpServletRequest request) {
//下载文件
urlList.stream().forEach(this::downloadFile);
//把下载的图片文件夹压缩
downloadZip(request, response);
}
/**
* 下载文件
* @param attachmentVo 文件
*/
public void downloadFile(String fileUrl){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//获取连接
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(3*1000);
//设置请求头
connection.setRequestProperty(
"User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
//获取输入流
inputStream = connection.getInputStream();
// 创建一个文件夹 download
File fileDir = new File(downloadDir);
if(!fileDir.exists()){
fileDir.mkdir();
}
// 将文件下载到download文件夹下 文件下载保存路径
String filePath = downloadDir + "/" + 此处为文件名称,可通过传入也可通过地址字符串截取;
// 通过路径创建文件
File file = new File(filePath);
//创建文件,存在覆盖
file.createNewFile();
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
log.error("文件下载出错:" + e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
log.error("关闭流出错:" + e);
}
}
}
/**
* 下载压缩包
*/
public void downloadZip(HttpServletRequest request, HttpServletResponse res) {
OutputStream out = null;
File zip = null;
File fileDir = null;
ZipOutputStream zos = null;
BufferedInputStream bis = null;
try{
//多个文件进行压缩,批量打包下载文件 创建压缩文件需要的空的zip包
String zipName = "zip文件";
// zip文件存放路径
String zipFilePath = downloadZip + File.separator + zipName + ".zip";
fileDir = new File(downloadZip);
if(!fileDir.exists()){
fileDir.mkdir();
}
//压缩文件
zip = new File(zipFilePath);
//创建文件,存在覆盖
zip.createNewFile();
//创建zip文件输出流
zos = new ZipOutputStream(new FileOutputStream(zip));
this.zipFile(downloadDir, zos);
if (zos != null){
zos.close();
}
//将打包后的文件写到客户端,输出的方法同上,使用缓冲流输出
bis = new BufferedInputStream(new FileInputStream(zipFilePath));
byte[] buff = new byte[bis.available()];
bis.read(buff);
//IO流实现下载的功能
res.setCharacterEncoding("UTF-8");
// 设置下载内容类型application/octet-stream(二进制流,未知文件类型);
res.setContentType("application/octet-stream;charset=UTF-8");
//防止文件名乱码
String userAgent = request.getHeader("USER-AGENT");
//火狐浏览器
if (userAgent.contains("Firefox") || userAgent.contains("firefox")) {
zipName = new String(zipName.getBytes(), "ISO8859-1");
} else {
//其他浏览器
zipName = URLEncoder.encode(zipName, "UTF-8");
}
//设置下载的压缩文件名称
res.setHeader("Content-disposition", "attachment;filename=" + zipName);
//创建页面返回方式为输出流,会自动弹出下载框
out = res.getOutputStream();
//输出数据文件
out.write(buff);
FileUtil.deleteDir(downloadDir);
}catch

该代码示例展示了如何在JavaSpringMVC应用中通过Controller接收文件URL列表,然后服务层逐个下载文件到临时目录,再将这些文件压缩成ZIP供用户下载。文件下载和压缩过程中涉及到HTTP连接设置、文件流处理及文件操作。
最低0.47元/天 解锁文章
1077





