java 多个 输出流_java将多个文件打包压缩成输出流并下载

//将磁盘的多个文件打包成压缩包并输出流下载

@GetMapping(value = "/download1")public void download1(HttpServletRequest request, HttpServletResponse response) throwsException {

String outputFileName= "文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";//设置response参数

response.reset();

response.setContentType("content-type:octet-stream;charset=UTF-8");

response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "iso-8859-1"));

ServletOutputStream out=response.getOutputStream();

ZipArchiveOutputStream zous= newZipArchiveOutputStream(out);

zous.setUseZip64(Zip64Mode.AsNeeded);

File f1= new File("D:\\testfile\\aaa.png");

File f2= new File("D:\\testfile\\bbb.png");

List fileList = new ArrayList<>();

fileList.add(f1);

fileList.add(f2);for(File file : fileList) {

String fileName=file.getName();

InputStream inputStream= newFileInputStream(file);

ByteArrayOutputStream baos= newByteArrayOutputStream();byte[] buffer = new byte[1024];intlen;while ((len = inputStream.read(buffer)) != -1) {

baos.write(buffer,0, len);

}if (baos != null) {

baos.flush();

}byte[] bytes =baos.toByteArray();//设置文件名

ArchiveEntry entry = newZipArchiveEntry(fileName);

zous.putArchiveEntry(entry);

zous.write(bytes);

zous.closeArchiveEntry();if (baos != null) {

baos.close();

}

}if(zous!=null) {

zous.close();

}

}//将网络url资源文件的多个文件打包成压缩包并输出流下载

@GetMapping(value = "/download2")public void download2(HttpServletRequest request, HttpServletResponse response) throwsException {

String outputFileName= "文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";//设置response参数

response.reset();

response.setContentType("content-type:octet-stream;charset=UTF-8");

response.setHeader("Content-Disposition", "attachment;filename=" + new String((outputFileName).getBytes(), "iso-8859-1"));

ServletOutputStream out=response.getOutputStream();

ZipArchiveOutputStream zous= newZipArchiveOutputStream(out);

zous.setUseZip64(Zip64Mode.AsNeeded);

String path1= "http://www.baidu.com/img/bd_logo1.png";

String path2= "http://www.baidu.com/img/bd_logo1.png";

List pathList = new ArrayList<>();

pathList.add(path1);

pathList.add(path2);for(String path : pathList) {

String fileName= UUID.randomUUID() + ".png";

InputStream inputStream=getInputStreamFromUrl(path);

ByteArrayOutputStream baos= newByteArrayOutputStream();byte[] buffer = new byte[1024];intlen;while ((len = inputStream.read(buffer)) != -1) {

baos.write(buffer,0, len);

}if (baos != null) {

baos.flush();

}byte[] bytes =baos.toByteArray();//设置文件名

ArchiveEntry entry = newZipArchiveEntry(fileName);

zous.putArchiveEntry(entry);

zous.write(bytes);

zous.closeArchiveEntry();if (baos != null) {

baos.close();

}

}if(zous!=null) {

zous.close();

}

}/*** 经过网络地址获取文件InputStream

*

*@parampath 地址

*@return

*/

public staticInputStream getInputStreamFromUrl(String path) {

URL url= null;

InputStream is= null;try{

url= newURL(path);

}catch(MalformedURLException e) {

e.printStackTrace();

}try{

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

conn.setDoInput(true);

conn.connect();

is=conn.getInputStream();

}catch(IOException e) {

e.printStackTrace();

}returnis;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是在 Java 中将多个文件打包成 ZIP 压缩文件并发送给客户端的示例代码: ``` import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileZipServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取需要打包文件列表 File[] files = getFilesToZip(); // 设置响应头,提示浏览器下载文件 response.setHeader("Content-Disposition", "attachment; filename=files.zip"); // 创建 ZIP 输出 ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream())); // 循环将文件写入 ZIP 压缩文件中 for (File file : files) { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { // 创建 ZIP 条目 ZipEntry entry = new ZipEntry(file.getName()); zos.putNextEntry(entry); // 写入文件到 ZIP 压缩文件中 int count; byte[] data = new byte[1024]; while ((count = bis.read(data, 0, 1024)) != -1) { zos.write(data, 0, count); } } } // 关闭 ZIP 输出 zos.close(); } private File[] getFilesToZip() { // 这里应该是从本地文件系统中获取需要打包文件列表的逻辑 return new File[] {new File("file1.txt"), new File("file2.txt")}; } } ``` 在这个例子中,我们使用了 `ZipOutputStream` 类来创建 ZIP 压缩文件,并使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值