java 多重压缩下载_Java批量压缩下载

public voiddownloadAllFiles(HttpServletRequest request, HttpServletResponse response) {//获取要下载的文件对应的信息ID-选中文件ID拼接的字符串

String lessionIdStr = request.getParameter("fileIds");//第一个文件的文件名

String firstFileName = "";

List downLoadFiles = new LinkedList();if(StringUtil.isNotEmpty(lessionIdStr)) {int end = lessionIdStr.lastIndexOf(",");if (end > 0) {if (end == lessionIdStr.length() - 1) {

lessionIdStr= lessionIdStr.substring(0, end);

}

String[] ids= lessionIdStr.split(",");for (int i = 0; i < ids.length; i++) {//循环获取每一个文件信息

UploadFileInfo fileInfo =uploadFileInfoService.selectByPrimaryKey(ids[i]);if (fileInfo != null) {

downLoadFiles.add(fileInfo);

}if (i == 0) {

firstFileName= fileInfo.getFileName().substring(0, fileInfo.getFileName().lastIndexOf("."));

}

}

}else{//循环获取每一个文件信息

UploadFileInfo fileInfo =uploadFileInfoService.selectByPrimaryKey(lessionIdStr);if (fileInfo != null) {

downLoadFiles.add(fileInfo);

}

firstFileName= fileInfo.getFileName().substring(0, fileInfo.getFileName().lastIndexOf("."));

}

}//有数据可以下载

if (downLoadFiles.size() != 0) {//进行预处理

preProcess(firstFileName, response);//压缩处理

writeZip(downLoadFiles);//下载文件

downFile(response);//删除临时压缩文件

afterProcess();

}

}//压缩文件输出流

privateZipOutputStream out;//临时压缩文件存储路径

privateString filePath;/*** 描述: 预处理

* 参数:@paramfirseFileName 批量下载的第一个文件名

* 参数:@paramresponse*/

private voidpreProcess(String firseFileName, HttpServletResponse response) {//压缩文件名称

String zipName = "【批量下载】" + firseFileName + "等.zip";

filePath= UPLOAD_PATH +zipName;try{//初始化压缩文件输出流

out = new ZipOutputStream(newFileOutputStream(filePath));//清空输出流(在迅雷下载不会出现一长窜)

response.reset();//设置响应头和客户端保存文件名

response.setCharacterEncoding("utf-8");

response.setContentType("multipart/form-data");//!!!new String(zipName.getBytes("GBK"), "8859_1") 如果文件包含中文,必须进行转换,否则下载后的文件名是乱码格式的

response.setHeader("Content-Disposition", "attachment;fileName=" + new String(zipName.getBytes("GBK"), "8859_1"));

}catch(IOException e) {

e.printStackTrace();

}

}/*** 描述: 压缩处理

* 参数:@paramdownloadFiles 批量下载的文件数据集合*/

private void writeZip(ListdownloadFiles) {byte[] buf = new byte[2048];int len = 0;try{for(UploadFileInfo fileInfo : downloadFiles) {//获取上传文件

File file = new File(UPLOAD_PATH.substring(0, UPLOAD_PATH.lastIndexOf("upload")) +fileInfo.getFilePath());if (!file.isFile()) {continue;

}//设置编码

out.setEncoding(System.getProperty("sun.jnu.encoding"));//设置压缩文件名称

ZipEntry ze = newZipEntry(fileInfo.getFileName());//加入到输出流中

out.putNextEntry(ze);//对源文件进行读取并输出

FileInputStream fis = newFileInputStream(file);while ((len = fis.read(buf)) != -1) {

out.write(buf,0, len);

}//刷新(必须要有)

out.flush();

out.closeEntry();

fis.close();

}if (out != null) {

out.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}/*** 描述: 下载临时压缩文件

* 参数:@paramresponse*/

private voiddownFile(HttpServletResponse response) {try{

File file= newFile(filePath);if(file.exists()) {

InputStream ins= newFileInputStream(filePath);//放到缓冲流里面

BufferedInputStream bins = newBufferedInputStream(ins);//获取文件输出IO流

OutputStream outs =response.getOutputStream();

BufferedOutputStream bouts= newBufferedOutputStream(outs);int bytesRead = 0;byte[] buffer = new byte[1024];//开始向网络传输文件流

while ((bytesRead = bins.read(buffer)) != -1) {

bouts.write(buffer,0, bytesRead);

}//这里一定要调用flush()方法

bouts.flush();

ins.close();

bins.close();

outs.close();

bouts.close();

}

}catch(IOException e) {

logger.error("文件下载出错", e);

}

}/*** 描述: 删除临时压缩文件*/

private voidafterProcess() {//删除源文件

File tempZip=newFile(filePath);if(tempZip.exists()) {

tempZip.delete();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。   public class ZipDemo extends JFrame{   JFileChooser fileChooser; //文件选择器   JList fileList; //待压缩的文件列表   Vector files; //文件数据(待压缩文件)   JButton jbAdd; //增加文件按钮   JButton jbDelete; //删除文件按钮   JButton jbZip; //压缩按钮   JTextField target; //目标文件文本域   public ZipDemo(){   super("用ZIP压缩多个文件"); //调用父类构造函数   fileChooser=new JFileChooser(); //实例化文件选择器   files=new Vector(); //实例化文件数据Vector   fileList=new JList(files); //实例化已选择文件列表   jbAdd=new JButton("增加"); //实例化按钮组件   jbDelete=new JButton("删除");   jbZip=new JButton("压缩");   target=new JTextField(18);   JPanel panel=new JPanel(); //实例化面板,用于容纳按钮   panel.add(jbAdd); //增加组件到面板上   panel.add(jbDelete);   panel.add(jbZip);   JPanel panel2=new JPanel();   panel2.add(new JLabel("目标文件"));   panel2.add(target);   JScrollPane jsp=new JScrollPane(fileList);   Container container=getContentPane(); //得到容器   container.add(panel2,BorderLayout.NORTH); //增加组件到容器   container.add(jsp,BorderLayout.CENTER);   container.add(panel,BorderLayout.SOUTH);   jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //设置边界
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值