Compressed file in ZIP format, available for download.

Source: Internet
Author: User

 PackageCom.bstd.sys.server;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.FontMetrics;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.io.UnsupportedEncodingException;ImportJava.net.URLEncoder;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Date;Importjava.util.List;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipOutputStream;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportCom.bstd.util.tool.MidLayer; Public classDownimgextendsHttpServlet {PrivateString FilePath =NULL; PrivateString downfilepath=NULL; /*** servlet Standard Get method **/     Public voiddoget (httpservletrequest req, HttpServletResponse Rep) {Try {                      //1 Pack first.                      This. Createzipfile (req, rep); //2 is available for download.                      This. DOWNIMG (req, rep);//Download Image}Catch(IOException e) {e.printstacktrace (); }    }    /*** Servlet Standard POST method **/     Public voidDoPost (httpservletrequest req, HttpServletResponse Rep)throwsservletexception, IOException {doget (req, rep); }     Public voidCreatezipfile (httpservletrequest req, HttpServletResponse Rep)throwsioexception{String Nowpath= System.getproperty ("User.dir"); FilePath= Nowpath.replace ("Bin", "WebApps"); FilePath+ = "\" + "Tongjiimage"; File File=NewFile (FilePath);//define a folder to compressdownfilepath=filepath+ "\ \ chart. zip";//Define compressed file namesFile ZipFile =NewFile (Downfilepath); InputStream input=NULL;//define file input streamZipoutputstream zipout =NULL;//declaring a compressed stream objectZipOut =NewZipoutputstream (NewFileOutputStream (ZipFile)); Zipout.setcomment ("Chart package, provide download");//Setting Comments            inttemp = 0 ; if(File.isdirectory ()) {//determine if it is a folderFile lists[] = File.listfiles ();//List all Files                 for(inti=0;i<lists.length;i++) {input=NewFileInputStream (Lists[i]);//defining the input stream for a fileZipout.putnextentry (NewZipEntry (File.getname ()+file.separator+lists[i].getname ()));//set ZipEntry Object                     while((Temp=input.read ())!=-1) {//Read ContentZipout.write (temp);//Compressed Output} input.close (); //close the input stream}} zipout.close (); //turn off the output stream            }     Public voiddownimg (httpservletrequest req, HttpServletResponse Rep) {Try {            //download the file in the form of a stream. InputStream FIS =NewBufferedinputstream (NewFileInputStream (Downfilepath)); byte[] buffer =New byte[Fis.available ()];            Fis.read (buffer);            Fis.close (); //Empty ResponseRep.reset (); OutputStream toclient=NewBufferedoutputstream (Rep.getoutputstream ()); Rep.setcontenttype ("Application/zip"); //If the output is the Chinese name of the file, here will be used Urlencoder.encode method to handleRep.setheader ("Content-disposition", "Attachment;filename=" +urlencoder.encode ("chart. zip", "UTF-8"));            Toclient.write (buffer);            Toclient.flush ();            Toclient.close (); } Catch(IOException ex) {ex.printstacktrace (); }finally{//after downloading, delete                 Try {                        //file F = new file ("E:\\1111.zip"); //F.delete ();}Catch(Exception e) {e.printstacktrace (); }            }    }         }

Compressed file in ZIP format, available for 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.