HttpServletResponse 前端响应压缩文件And在压缩包中新建文件夹

本文介绍了一种使用Java实现文件压缩的方法,通过Apache Commons Compress库进行操作,并展示了如何利用ZipArchiveOutputStream和ZipOutputStream实现文件的压缩。同时,文章还提供了在控制器层实现文件下载的具体代码示例。

(1)---pox配置
   <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-compress</artifactId>
             <version>1.9</version>

   </dependency>

 (2)---建立公共方法

/**
 *Zip文件工具类
 *
 *@author @authorwenfei.fang
 */
publicclassZipFileUtil {
     
     /**
      *通过 try-with-resources 的语法来创建流,可以安全的自动关闭
      * @authorwenfei.fang
      *@paramZipArchiveOutputStream  压缩的文件流
      *@paramsonFileName      向压缩文件插入的文件名 test.txt
      *@paraminStreamFile      test.txt文件输入流
      *@throwsIOException     Io异常
      */
     publicstaticvoidcompressFilesZip(ZipArchiveOutputStream zaos, String SonFileName, InputStream inStreamFile) {
          if(SonFileName!=null&&SonFileName.length() > 0) {
                   try{
                        zaos.setUseZip64(Zip64Mode.AsNeeded);
                        ZipArchiveEntryzipArchiveEntry=newZipArchiveEntry(SonFileName);
                        zaos.putArchiveEntry(zipArchiveEntry);
                        try{
                             byte[]buffer=newbyte[1024 * 5];
                             intlen= -1;
                             while((len=inStreamFile.read(buffer)) != -1) {
                                  zaos.write(buffer, 0,len);
                             }
                             zaos.closeArchiveEntry();
                        }catch(Exceptione) {
                             thrownewRuntimeException(e);
                        }finally{
                             if(inStreamFile!=null)
                                  inStreamFile.close();
                        }
                        zaos.finish();
                   }catch(Exceptione) {
                        thrownewRuntimeException(e);
                   }finally{
                        try{
                             if(inStreamFile!=null) {
                                  inStreamFile.close();
                             }
                        }catch(IOExceptione) {
                             thrownewRuntimeException(e);
                        }
                   }

              }

          

     }
     
     /**
      * @authorwenfei.fang
      *@paramzipOutputStream  压缩的文件流
      *@paramsonFileName      向压缩文件插入的文件名 test.txt
      *@paraminStreamFile      test.txt文件输入流
      *@throwsIOException     Io异常
      */
     publicstatic void writeFile(ZipOutputStreamzipOutputStream, StringsonFileName, InputStreaminStreamFile)throwsIOException {
            ZipEntry ze=newZipEntry(sonFileName);
            zipOutputStream.putNextEntry(ze);
            byte[]buffer=newbyte[1204];
            intlen= -1;
           while((len=inStreamFile.read(buffer)) != -1) {
            System.err.println(len);
            zipOutputStream.write(buffer, 0,len);
            }
           inStreamFile.close();
      }

     
     /**
      *@authorwenfei.fang
      *@用apachecommentsio包中的IOUtils中的方法
      *@paramzipOutputStream  压缩的文件流
      *@paramsonFileName      向压缩文件插入的文件名 test.txt
      *@paraminStreamFile      test.txt文件输入流
      *@throwsIOException     Io异常
      *@messageCopy bytes from a large (over 2GB) InputStream to an OutputStream. 将字节超过2G的字节输入流复制到输出流中
      *  //IOUtils.copyLarge(input, output)
      */
     publicstaticvoidwriteFileUseIOUtils(ZipOutputStreamzipOutputStream, StringsonFileName, InputStreaminStreamFile)throwsIOException {
            ZipEntryze=newZipEntry(sonFileName);
           zipOutputStream.putNextEntry(ze);
            IOUtils.copy(inStreamFile,zipOutputStream);
            IOUtils.closeQuietly(inStreamFile);
      }

}



在control层的操作     

@RequestMapping(value ="/api/fblMaster/downloadNet23")
     public@ResponseBodyString downloadNet2(HttpServletResponse response)throwsMalformedURLException {
            //通过给定的URL字符串创建URL
             URLurl=newURL("http://10.152.244.25:16200/cs/idcplg?IdcService=GET_FILE&dID=23378&dDocName=GGSTEST16200022613&allowInterrupt=1");
            try{
                     URLConnectionconn=url.openConnection();
                    InputStreaminStreamFile=conn.getInputStream();
                   
                    URLConnectionconn1=url.openConnection();
                    InputStreaminStreamFile1=conn1.getInputStream(); 
                   
                    URLConnectionconn2=url.openConnection();
                    InputStreaminStreamFile2=conn2.getInputStream(); 
                   
                    URLConnectionconn21=url.openConnection();
                    InputStreaminStreamFile21=conn21.getInputStream(); 
                 
                  response.setHeader("Content-Disposition","attachment; filename=Barcode_Label_Approval_Verification"+ String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".zip");
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("application/x-msdownload");

                   OutputStream out=response.getOutputStream();
                   ZipOutputStream zos=newZipOutputStream(out);   
          
          ZipFileUtil.writeFileUseIOUtils(zos,"test1.jpg",inStreamFile);
                   ZipFileUtil.writeFileUseIOUtils(zos,"sonFileName1/test1.jpg",inStreamFile1);
                   ZipFileUtil.writeFileUseIOUtils(zos,"sonFileName2/test2.jpg",inStreamFile2);
                   ZipFileUtil.writeFileUseIOUtils(zos,"sonFileName2/test1.jpg",inStreamFile21);
             //关闭文件流(如果不关闭可能在压缩包中的文件只有名称没有文件内容)    
           IOUtils.closeQuietly(zos);
             }catch(FileNotFoundExceptione) {
                e.printStackTrace();
             }catch(IOExceptione) {
                e.printStackTrace();
             }
            returnnull;
         }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值