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.