多文件打包下载以及单文件下载

今天项目中需要多文件打包下载和单文件下载的功能,以下做一些总结。

原代码:

  1 package com.hlbj.utils;
  2
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.InputStream;
  7 import java.io.OutputStream;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 import java.util.zip.ZipEntry;
 11 import java.util.zip.ZipOutputStream;
 12 import javax.servlet.http.HttpServletResponse;
 13 import org.apache.commons.lang3.StringUtils;
 14
 15 import com.app.core.common.Config;
 16 import com.hlbj.pdf.component.GeneratePdf;
 17
 18 public class DownLoadUitls {
 19         //图片地址
 20     private static String physicalPath = "";        //多文件压缩下载
 21     public static boolean downLoadPic(String filePathstr, String fileNamestr, HttpServletResponse response) {
 22         boolean isgood = false;
 23
 24         if (StringUtils.isBlank(filePathstr) || StringUtils.isBlank(fileNamestr)) {
 25             return isgood;
 26         }
 27
 28         String[] filePaths = filePathstr.split(",");
 29         String[] fileNames = fileNamestr.split(",");
 30
 31         if (filePaths == null || filePaths.length == 0 || fileNames == null || fileNames.length == 0) {
 32             return isgood;
 33         }
 34
 35         String classpath = "";
 36         if (StringUtils.isNoneBlank(physicalPath)) {
 37             classpath = physicalPath;
 38         } else {
 39             classpath = GeneratePdf.class.getClassLoader().getResource("").getPath();
 40             classpath = classpath.substring(0, classpath.lastIndexOf("hlbj_api") - 1);
 41         }
 42
 43         List<FileInputStream> inputs = new ArrayList<FileInputStream>();
 44         try {
 45             for (String path : filePaths) {
 46                 String str = classpath + path.substring(path.indexOf("/s4m3files"));
 47                 File file = new File(str);
 48                 if (file.exists()) {
 49                     FileInputStream input = new FileInputStream(file);
 50                     inputs.add(input);
 51                 }
 52             }
 53
 54             // ZIP打包图片
 55             String downPath = classpath + "/images.zip";
 56             File zipFile = new File(downPath);
 57             byte[] buf = new byte[1024];
 58             int len;
 59             ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zipFile));
 60             for (int i = 0; i < inputs.size(); i++) {
 61                 FileInputStream in = inputs.get(i);
 62                 zout.putNextEntry(new ZipEntry(fileNames[i]));
 63                 while ((len = in.read(buf)) > 0) {
 64                     zout.write(buf, 0, len);
 65                 }
 66                 zout.closeEntry();
 67                 in.close();
 68             }
 69             zout.close();
 70
 71             // 下载图片
 72             FileInputStream zipInput = new FileInputStream(zipFile);
 73             OutputStream out = response.getOutputStream();
 74             response.setContentType("application/octet-stream");
 75             response.setHeader("Content-Disposition", "attachment; filename=images.zip");
 76             while ((len = zipInput.read(buf)) != -1) {
 77                 out.write(buf, 0, len);
 78             }
 79             zipInput.close();
 80             out.flush();
 81             out.close();
 82             // 删除压缩包
 83             zipFile.delete();
 84             isgood = true;
 85         } catch (Exception e) {
 86             // TODO Auto-generated catch block
 87             e.printStackTrace();
 88         }
 89
 90         return isgood;
 91     }
 92     //单文件下载
 93     public static void downLoadPdf(String filePathstr, HttpServletResponse response) {
 94         String classpath = "";
 95         if (StringUtils.isNoneBlank(physicalPath)) {
 96             classpath = physicalPath;
 97         } else {
 98             classpath = GeneratePdf.class.getClassLoader().getResource("").getPath();
 99             classpath = classpath.substring(0, classpath.lastIndexOf("hlbj_api") - 1);
100         }
101         filePathstr= classpath + filePathstr.substring(filePathstr.indexOf("/pdf"));
102         // 下载本地文件
103         // path是指欲下载的文件的路径。
104         File file = new File(filePathstr);
105         // 取得文件名。
106         String filename = file.getName();
107         // 读到流中
108         InputStream inStream;
109
110
111         try {
112             inStream = new FileInputStream(filePathstr);
113             // 设置输出的格式
114             response.reset();
115             response.setContentType("bin");
116             response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
117             // 循环取出流中的数据
118             byte[] b = new byte[100];
119             int len;
120             while ((len = inStream.read(b)) > 0){
121                 response.getOutputStream().write(b, 0, len);
122             }
123             inStream.close();
124         } catch (Exception e1) {
125             // TODO Auto-generated catch block
126             e1.printStackTrace();
127         } // 文件的存放路径
128     }
129 }

原文地址:https://www.cnblogs.com/huanlingjisi/p/9082607.html

时间: 2024-08-03 19:11:23

多文件打包下载以及单文件下载的相关文章

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 &amp;&amp; Linux下的ZipArchive配置开启压缩

PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流.这里整理一下常用的示例供参考. 一.解压缩zip文件 ? 1 2 3 4 5 6 7 8 9 10 11 $zip = new ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表示处理的

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 http://my.oschina.net/junn/blog/104464 PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流.这里整理一下常用的示例供参考. 一.解压缩zip文件 ? 1 2 3 4 5 6 7 8 9 10 11 $zip = new ZipAr

servlet实现多文件打包下载

当用户一次下载多个文件时.普通情况是,每下载一个文件,均要弹出一个下载的对话框.这给用户造成了非常大不便. 比較理想的情况是,用户选择多个文件后.server后端直接将多个文件打包为zip.以下贴出实现代码. 前端Javascript代码(使用Javascript创建表单.通过提交表单的方式訪问后端的MultiDownload): var tmpForm = document.createElement("form"); tmpForm.id = "form1" ;

Struts---多文件上传、单文件下载

struts.xml 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 &l

java 实现多文件打包下载

jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.action = "${pageContext.request.contextPath}/DocumentController/downloadattached"; options.argname1="id"; options.argvalue1=id.join(','); f

文件打包为zip格式文件下载

整个思路是这样的: 1.查询数据库中的文件流放到datatable中2.循环datatable将文件流一个个生成文件,放到对应的文件夹中,3.下载某个文件夹下的所有文件a.循环这个文件夹下的所有文件,调用zip()方法压缩到zipSteam中b.将zipStream流保存为一个.zip文件4.返回给前端压缩成功5.前端用window.open(“压缩文件.zip”)下载压缩文件 这个程序是几年前写的,比较绕,不是个最佳方法,应该有更好的. 优化思路: 1.将数据库中查询出来的文件流直接调用zip

de4dot3.14更新文件打包下载

刚发现de4dot更新了,虽然只是10月份的文件更新,并未发布新的release,但好多人还不会编译... 关于de4dot有何功能就不再讲了. 本文主要提供编译通过后的打包文件下载. 首先下载de4dot源代码(点击这里),同时还需要下载作者的另一个library(dnlib). 解压 dnlib 后将 dnlib-master 文件夹中文件复制到 \de4dot-master\dnlib 中用 Visual Studio (最低版本2010)打开即可. 本文地址:http://www.cnb

Java批量文件打包下载

经常遇到选择多个文件进行批量下载的情况,可以先将选择的所有的文件生成一个zip文件,然后再下载,该zip文件,即可实现批量下载,但是在打包过程中,常常也会出现下载过来的zip文件中里面有乱码的文件名,通过使用ant.jar中的org.apache.tools.zip里的ZipOutPutStream为实现编码的设置. 代码如下: ant包引用 [html] view plain copy print? <span style="font-size:14px">Xml代码 &

PHP扩展类ZipArchive实现压缩Zip文件和文件打包下载

1 <?php 2 /** 3 * 关于文件压缩和下载的类 4 * @author tycell 5 * @version 1.0 6 */ 7 class zip_down{ 8 9 protected $file_path; 10 /** 11 * 构造函数 12 * @param [string] $path [传入文件目录] 13 */ 14 public function __construct($path){ 15 $this->file_path=$path; //要打包的根目录