首页 / JAVA / Java下载压缩文件
Java下载压缩文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java下载压缩文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4063字,纯文字阅读大概需要6分钟。
内容图文
Java下载压缩文件
List<File> files = new ArrayList<>();
String fileName = URLEncoder.encode("文件名" + dateToDayString + ".zip", "UTF-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setDateHeader("Expires", (System.currentTimeMillis() + 10000));
// 使用有权限目录
String videoPath = "csv" + "/";
zipComponent.downLoadFiles(files, response, nasPath + videoPath, fileName);
package com.sf.argus.web.component;
import java.io.*;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ZipComponent {
private String cosDomainProxy;
private String nasPath;
private String csv;
public HttpServletResponse downLoadFiles(List<File> files, HttpServletResponse response, String zipPath,String zipName) {
String zipFilename = zipPath + zipName;
File file = new File(zipFilename);
try (FileOutputStream fileOut = new FileOutputStream(file)) {
log.info("准备新建下载");
if (!file.exists()) {
file.createNewFile();
log.info("新建完成");
}
response.reset();
// 创建文件输出流
ZipOutputStream zipOut = new ZipOutputStream(fileOut);
zipFile(files, zipOut);
zipOut.close();
fileOut.close();
log.info("压缩文件是否生成:{} 文件大小:{}" , file.exists(),file.length());
return downloadZip(file, response);
} catch (Exception e) {
log.error("downLoadFiles error", e);
}
return response;
}
/**
* 把接受的全部文件打成压缩包
*/
public void zipFile(List<File> files, ZipOutputStream outputStream) {
int size = files.size();
for (int i = 0; i < size; i++) {
File file = files.get(i);
zipFile(file, outputStream);
}
}
/**
* 根据输入的文件与输出流对文件进行打包
*/
public void zipFile(File inputFile, ZipOutputStream outputStream) {
InputStream inputStream = null;
BufferedInputStream bins = null;
try {
String path = inputFile.getPath();
StringBuilder sb = new StringBuilder();
sb.append(cosDomainProxy).append(path);
HttpRequest get = HttpUtil.createGet(sb.toString());
HttpResponse execute = get.execute();
log.info("path:{} from proxy read file status:{}", path, execute.getStatus());
if (!execute.isOk()) {
return;
}
inputStream = execute.bodyStream();
bins = new BufferedInputStream(inputStream, 512);
ZipEntry entry = new ZipEntry(inputFile.getName());
outputStream.putNextEntry(entry);
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
outputStream.write(buffer, 0, nNumber);
}
} catch (Exception e) {
log.error("zipFile error", e);
} finally {
// 关闭创建的流对象
try {
if (bins != null) {
bins.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
log.error("zipFile error", e);
}
}
}
public HttpServletResponse downloadZip(File file, HttpServletResponse response) {
if (!file.exists()) {
log.info("待压缩的文件目录:{}不存在", file);
} else {
try (InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()))) {
// 以流的形式下载文件。
byte[] buffer = new byte[fis.available()];
while (fis.read(buffer) > 0) {
response.reset();
try (OutputStream toClient = new BufferedOutputStream(response.getOutputStream())) {
response.setContentType("application/octet-stream");
// 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
toClient.write(buffer);
toClient.flush();
}
}
} catch (IOException ex) {
log.error("downloadZip error", ex);
} finally {
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
log.error("delete file error", e);
}
}
}
return response;
}
}
内容总结
以上是互联网集市为您收集整理的Java下载压缩文件全部内容,希望文章能够帮你解决Java下载压缩文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。