`
tidelee
  • 浏览: 1390 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

远程文件打包下载

    博客分类:
  • java
阅读更多

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();     
    }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics