java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)

1.压缩代码:

  1. import java.io.BufferedInputStream;  
  2. import java.io.BufferedOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.util.zip.ZipEntry;  
  9. import java.util.zip.ZipOutputStream;  
  10. /** 
  11.  * 将文件夹下面的文件 
  12.  * 打包成zip压缩文件 
  13.  *  
  14.  * @author admin 
  15.  * 
  16.  */  
  17. public final class FileToZip {  
  18.   
  19.     private FileToZip(){}  
  20.       
  21.     /** 
  22.      * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下 
  23.      * @param sourceFilePath :待压缩的文件路径 
  24.      * @param zipFilePath :压缩后存放路径 
  25.      * @param fileName :压缩后文件的名称 
  26.      * @return 
  27.      */  
  28.     public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){  
  29.         boolean flag = false;  
  30.         File sourceFile = new File(sourceFilePath);  
  31.         FileInputStream fis = null;  
  32.         BufferedInputStream bis = null;  
  33.         FileOutputStream fos = null;  
  34.         ZipOutputStream zos = null;  
  35.           
  36.         if(sourceFile.exists() == false){  
  37.             System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.");  
  38.         }else{  
  39.             try {  
  40.                 File zipFile = new File(zipFilePath + "/" + fileName +".zip");  
  41.                 if(zipFile.exists()){  
  42.                     System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");  
  43.                 }else{  
  44.                     File[] sourceFiles = sourceFile.listFiles();  
  45.                     if(null == sourceFiles || sourceFiles.length<1){  
  46.                         System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");  
  47.                     }else{  
  48.                         fos = new FileOutputStream(zipFile);  
  49.                         zos = new ZipOutputStream(new BufferedOutputStream(fos));  
  50.                         byte[] bufs = new byte[1024*10];  
  51.                         for(int i=0;i<sourceFiles.length;i++){  
  52.                             //创建ZIP实体,并添加进压缩包  
  53.                             ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  
  54.                             zos.putNextEntry(zipEntry);  
  55.                             //读取待压缩的文件并写进压缩包里  
  56.                             fis = new FileInputStream(sourceFiles[i]);  
  57.                             bis = new BufferedInputStream(fis, 1024*10);  
  58.                             int read = 0;  
  59.                             while((read=bis.read(bufs, 01024*10)) != -1){  
  60.                                 zos.write(bufs,0,read);  
  61.                             }  
  62.                         }  
  63.                         flag = true;  
  64.                     }  
  65.                 }  
  66.             } catch (FileNotFoundException e) {  
  67.                 e.printStackTrace();  
  68.                 throw new RuntimeException(e);  
  69.             } catch (IOException e) {  
  70.                 e.printStackTrace();  
  71.                 throw new RuntimeException(e);  
  72.             } finally{  
  73.                 //关闭流  
  74.                 try {  
  75.                     if(null != bis) bis.close();  
  76.                     if(null != zos) zos.close();  
  77.                 } catch (IOException e) {  
  78.                     e.printStackTrace();  
  79.                     throw new RuntimeException(e);  
  80.                 }  
  81.             }  
  82.         }  
  83.         return flag;  
  84.     }  
  85.       
  86.     public static void main(String[] args){  
  87.         String sourceFilePath = "D:\\TestFile";  
  88.         String zipFilePath = "D:\\tmp";  
  89.         String fileName = "12700153file";  
  90.         boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);  
  91.         if(flag){  
  92.             System.out.println("文件打包成功!");  
  93.         }else{  
  94.             System.out.println("文件打包失败!");  
  95.         }  
  96.     }  
  97.       
  98. }  

2.spring MVC下载

a.

    @RequestMapping(value = "/downloadFile")
    @ResponseBody
    public void downloadFile (HttpServletResponse response) {
OutputStream os = null;
try {
       os = response.getOutputStream();
       File file = new File("D:/javaweb/demo.txt");
       // Spring工具获取项目resources里的文件
       File file2 = ResourceUtils.getFile("classpath:shell/init.sh");
if(!file.exists()){
          return;
       }
response.reset(); response.setHeader("Content-Disposition", "attachment;filename=demo.txt"); response.setContentType("application/octet-stream; charset=utf-8"); os.write(FileUtils.readFileToByteArray(file)); } catch (Exception e) { e.printStackTrace(); }finally{ IOUtils.closeQuietly(os); } }
b.

/**
     * Spring下载文件
     * @param request
     * @throws IOException 
     */
    @RequestMapping(value="/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException{
     // 获取项目webapp目录路径下的文件 String path
= request.getSession().getServletContext().getRealPath("/"); File file = new File(path+"/soft/javaweb.txt"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "javaweb.txt"); return new ResponseEntity<byte[]>(org.apache.commons.io.FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED); }
  
<a target="_blank" href="/download">点击下载</a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值