To summarize, about Java download zip file and export the method, browser export.
String downloadname = "Download file name. zip"; Downloadname=Browsercharcodeutils.browsercharcodefun (Request, downloadname);//download file name garbled problem solved//To Package a file for download Try{OutputStream out=Response.getoutputstream (); byte[] data = Createzip ("/filestorage/download"));//server storage address response.reset (); Response.setheader ("Content-disposition", "attachment;filename=" +downloadname); Response.AddHeader ("Content-length", "" "+data.length); Response.setcontenttype ("Application/octet-stream;charset=utf-8"); Ioutils.write (data, out); Out.flush (); Out.close (); } Catch(Exception e) {e.printstacktrace (); }
Get download zip file stream
Public byte throws exception{ new bytearrayoutputstream (); New Zipoutputstream (outputstream); // Package The destination file as a Zip export New File (srcsource); A (Zip,file,""); ioutils.closequietly (Zip); return Outputstream.tobytearray (); }
Public voidA (zipoutputstream zip, file file, String dir)throwsException {//If the current folder is present, further processing if(File.isdirectory ()) {//Get file list informationfile[] Files =File.listfiles (); //Add a folder to the next level package directoryZip.putnextentry (NewZipEntry (dir + "/")); Dir= Dir.length () = = 0? "": Dir + "/"; //loops package files in a folder for(inti = 0; i < files.length; i++) {A (zip, files[i], dir+ Files[i].getname ());//Recursive processing } } Else{//current is file, package processing//file input streamBufferedinputstream bis =NewBufferedinputstream (Newfileinputstream (file)); ZipEntry entry=NewZipEntry (dir); Zip.putnextentry (entry); Zip.write (Fileutils.readfiletobytearray (file)); ioutils.closequietly (bis); Zip.flush (); Zip.closeentry (); } }
Java generates a ZIP file and exports