Javaweb compress multiple files and download

Source: Internet
Author: User

        //file nameString[] names={"one.jpg", "two.jpg", "three.jpg", "four.jpg"}; //Four file streamsFileInputStream INPUT1 =NewFileInputStream (NewFile ("FileName path")); FileInputStream Input2=NewFileInputStream (NewFile ("FileName path")); FileInputStream INPUT3=NewFileInputStream (NewFile ("FileName path")); FileInputStream Input4=NewFileInputStream (NewFile ("FileName path")); Fileinputstream[] Inputs={INPUT1,INPUT2,INPUT3,INPUT4}; //Zip packaging PicturesFile ZipFile =NewFile ("Storage path for compressed files"); byte[] buf =New byte[1024]; intLen; Zipoutputstream Zout=NewZipoutputstream (NewFileOutputStream (ZipFile));  for(inti = 0; i < inputs.length; i++) {FileInputStream in=Inputs[i]; Zout.putnextentry (NewZipEntry (Names[i]));  while(len = In.read (buf)) > 0) {zout.write (buf,0, Len);              } zout.closeentry ();          In.close ();                        } zout.close (); //Download ImageFileInputStream Zipinput =NewFileInputStream (ZipFile); OutputStream out=Response.getoutputstream (); Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition", "attachment; Filename=images.zip ");  while((Len=zipinput.read (BUF))! =-1) {out.write (buf,0, Len);        } zipinput.close ();        Out.flush ();        Out.close (); //Delete a compressed packageZipfile.delete ();

Javaweb compress multiple files and download

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.