远程文件打包下载

1、根据网络地址获取输入流

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream inputStream = conn.getInputStream();

 2、多文件打包成ZIP,输出流到前台进行下载

       byte[] buffer = new byte[1024];  ;   
       ZipOutputStream out = new ZipOutputStream(response.getOutputStream());   
  
       //需要打包的文件  
       File[] file1 = {new File("e:/a.txt"),new File("e:/b.txt"),new File("e:/aa.txt"),new File("e:/bb.txt")};   
       for(int i=0;i<file1.length;i++) {   
           FileInputStream fis = new FileInputStream(file1[i]);  
           out.putNextEntry(new ZipEntry(file1[i].getName())); 
           int len;   
           //读入需要下载的文件的内容,打包到zip文件   
          while((len = fis.read(buffer))>0) {   
           out.write(buffer,0,len);    
          }   
           out.closeEntry();   
           fis.close();   
       }   
        out.flush();
        out.close();     
    }  

猜你喜欢

转载自tidelee.iteye.com/blog/2310363