Java FTPClient downloads multiple files

Source: Internet
Author: User

Recently, I had to download multiple files from the server and compress them at the same time. I checked the Internet for a long time without a line. Finally, I came up with the following method for reference only.

/*** Compressed download folder * @ param namelist download file List * @ param path download path * @ param zipname compressed file name */public void zipDownloadFile (HttpServletResponse response, List
 
  
Namelist, String path, String zipname) {byte [] buf = new byte [1024]; try {// save locally set response. addHeader ("Content-Disposition", "attachment; filename =" + URLEncoder. encode (zipname, sysEncoding) + ". zip "); response. setContentType ("application/x-zip-compressed"); // write the file ServletOutputStream sos = response. getOutputStream (); ZipOutputStream zipOut = new ZipOutputStream (new BufferedOutputStream (sos); for (String n Ame: namelist) {ZipEntry entry = new ZipEntry (name); zipOut. putNextEntry (entry); InputStream bis = this. getStream (path, name); if (bis! = Null) {int readLen =-1; while (readLen = bis. read (buf, 0, 1024 ))! =-1) {zipOut. write (buf, 0, readLen);} bis. close () ;}} zipOut. close ();} catch (Exception e) {e. printStackTrace ();}}
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.