| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package com.zhcs.dt.service.information.annualPlan;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.List;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class DownloadZip {
- // public static void batchDownLoadFile(HttpServletRequest request,HttpServletResponse response,String filename,String[] filepath,String[] documentname,String loginname){
- // byte[] buffer = new byte[1024];
- // Date date=new Date();
- // //生成zip文件存放位置
- // String strZipPath = Constant.exportAddress +loginname+date.getTime()+".zip";
- // File file=new File(Constant.exportAddress);
- // if(!file.isDirectory() && !file.exists()){
- // //创建单层目录
- // // f.mkdir();
- // // 创建多层目录
- // file.mkdirs();
- // }
- // try {
- // ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));
- // // 需要同时下载的多个文件
- // for (int i = 0; i < filepath.length; i++) {
- // File f=new File(filepath[i]);
- // FileInputStream fis = new FileInputStream(f);
- // System.out.println(documentname[i]);
- // out.putNextEntry(new ZipEntry(documentname[i]));
- // //设置压缩文件内的字符编码,不然会变成乱码
- // out.setEncoding("GBK");
- // int len;
- // // 读入需要下载的文件的内容,打包到zip文件
- // while ((len = fis.read(buffer)) > 0) {
- // out.write(buffer, 0, len);
- // }
- // out.closeEntry();
- // fis.close();
- // }
- // out.close();
- // PublicMethod.downLoadFile(request, response, strZipPath, filename+".zip");
- // File temp=new File(strZipPath);
- // if(temp.exists()){
- // temp.delete();
- // }
- // } catch (Exception e) {
- // System.out.println("文件下载错误");
- // }
- // }
-
-
- public static void downloadZip(HttpServletResponse response,HttpServletRequest request,List<String> paths,String path)throws Exception{
- OutputStream out = response.getOutputStream();
- File zip = new File("文档.zip");
- ZipFiles(paths, zip);
- FileInputStream inStream = new FileInputStream(zip);
- byte[] buf = new byte[4096];
- int readLength;
- while (((readLength = inStream.read(buf)) != -1)) {
- out.write(buf, 0, readLength);
- }
- inStream.close();
- out.flush();
- out.close();
- }
-
- public static void ZipFiles(List<String> paths, java.io.File zipfile) {
- byte[] buf = new byte[1024];
- try {
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
- zipfile));
- for (int i = 0; i < paths.size(); i++) {
- if (paths.get(i) != null && !paths.get(i).equals("")) {
- FileInputStream in = new FileInputStream(paths.get(i));
- File file = new File(paths.get(i));
- out.putNextEntry(new ZipEntry(file.getName()));
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- out.closeEntry();
- in.close();
- }
- }
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- finally{
- deleteFiles(paths);
- }
- }
-
- public static void deleteFiles(List<String> paths) {
- try {
- for (String string : paths) {
- File file = new File(string);
- file.delete();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|