Java implementation Edge Download edge compression

Source: Internet
Author: User

Requirements: Download pictures from the server.
Because of the problem of pre-project setup, the picture of each project is stored separately. Customer requirements according to the project download, implementation of the project sub-place, and then the local directory is the picture

@ResponseBody Public voidDownloadURL (httpservletresponse response, HttpServletRequest request) {String Sourcefilepath=properties.getstring ("Sourcefilepath");//the file path to downloadEavlproname = xxx;//Project NameString downloadname = "Xxx.zip";//Download file nameString Agent = Request.getheader ("user-agent");//Browser kernel information    Try {        if(Agent.contains ("MSIE") | | Agent.contains ("Trident")) {//IEDownloadname = Java.net.URLEncoder.encode (Downloadname, "UTF-8"); } Else{downloadname=NewString (Downloadname.getbytes ("UTF-8"), "Iso-8859-1"); }    } Catch(Exception e) {e.printstacktrace ();    } zip (Sourcefilepath, eavlproname, Response, downloadname); Response.getoutputstream (). Close (); //This should be useless.}Private voidZip (String soucefilename, String eavlproname, httpservletresponse response, string downloadname) {File file 
    =NewFile (soucefilename); Try{zip (file, eavlproname, response, downloadname); } Catch(IOException e) {e.printstacktrace (); }  }  Private voidZip (File soucefile, String eavlproname, httpservletresponse response, String downloadname)throwsIOException {Zipoutputstream out=NULL; //sets the compressed stream. Response.setheader ("Content-disposition", "attachment;filename=\" "+ downloadname +" \ ""); Try{ out=NewZipoutputstream (NewBufferedoutputstream (Response.getoutputstream ())); Out.setmethod (zipoutputstream.deflated); //set the compression method}Catch(Exception e) {e.printstacktrace (); } out.setencoding ("GBK"); Zip (Soucefile, out, Eavlproname, Eavlproname,NULL);    Out.closeentry (); Out.close (); }  Private voidZip (File soucefile, zipoutputstream out, string base, String eavlproname, Integer i)throwsIOException {if(Soucefile.isdirectory ()) {file[] files=Soucefile.listfiles (); if(Files.length! = 0) {            if(i = =NULL|| I! = 1) {//The local directory is not created first, and is created when the project exists under the local directory .Out.putnextentry (NewZipEntry (base + "/")); Base= Base.length () = = 0? "": Base + "/"; }            if(i = =NULL) {i= 0; } I++;  for(File file:files) {if(i = =NULL|| I! = 2) {//2 places under the project name. This does not create a directoryZip (file, out, base +file.getname (), eavlproname, i); }Else{String Picpathname=File.getname (); if(Picpathname.equals (Eavlproname)) {//file under the corresponding itemzip (file, out, base, eavlproname, i); }                }            }        }    } Else {          if(Base.length () > 0) {out.putnextentry (NewZipEntry (base)); } Else{out.putnextentry (NewZipEntry (Soucefile.getname ())); } FileInputStream in=NewFileInputStream (Soucefile); intb; byte[] by =New byte[1024];  while((b = in.read (by))! =-1) {Out.write (by,0, B);      } in.close (); }  } 

Reference: http://www.jianshu.com/p/7cabe09ce563

Java implementation Edge Download edge compression

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.