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