Public@ResponseBodyvoidExportexcel (HttpServletRequest request, httpservletresponse response, Khxxcxvo vo)throwsioexception{File csvfile= Createcsvfile (REQUEST,VO);//get the file you want to downloadBufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Response.setheader ("Content-disposition", "attachment; Filename= "+ Urlencoder.encode (Csvfile.getname ()," UTF-8 ")); Response.setheader ("Content-length", String.valueof (Csvfile.length ())); Bis=NewBufferedinputstream (NewFileInputStream (csvfile)); Bos=NewBufferedoutputstream (Response.getoutputstream ()); byte[] Buff =New byte[2048]; while(true) { intBytesread; if( -1 = = (Bytesread = bis.read (buff, 0, buff.length))) Break; Bos.write (Buff,0, Bytesread); } bis.close (); Bos.close (); }
Often encountered the choice of multiple files for bulk download, you can first select all the files to generate a zip file, and then download, the zip file, you can achieve bulk download, but in the packaging process, often also appear in the downloaded ZIP file contains garbled file name, The encoding is implemented by using the Zipoutputstream in the Org.apache.tools.zip in Ant.jar.
The code is as follows:
Ant Package Reference
<Dependency> <groupId>Ant</groupId> <Artifactid>Ant</Artifactid> <version>1.6.5</version> </Dependency>
Compress the downloaded Action code
Packagedemo.action; ImportJava.io.BufferedInputStream; ImportJava.io.BufferedOutputStream; ImportJava.io.File; ImportJava.io.FileInputStream; ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.InputStream; ImportJava.io.OutputStream; ImportJava.net.URLEncoder; ImportJavax.servlet.http.HttpServletResponse; ImportOrg.apache.log4j.Logger; ImportOrg.apache.struts2.ServletActionContext; ImportOrg.apache.tools.zip.ZipEntry; ImportOrg.apache.tools.zip.ZipOutputStream; ImportCom.opensymphony.xwork2.ActionSupport; /*** Bulk Download files: * Using org.apache.tools.zip.* in Ant.jar package to complete the compression, * Java native also has java.util.zip.* but the test can not take care of the compression * file The Chinese question in the file name *@authorYangcong **/ Public classBatchdownloadactionextendsActionsupport {PrivateLogger Log = Logger.getlogger (batchdownloadaction.class); Private Static FinalString FilePath = "d:\\"; Private Static Final LongSerialversionuid = -8694640030455344419l; PublicString Execute () {//the generated zip file is named Demo.zipString tmpfilename = "Demo.zip"; byte[] buffer =New byte[1024]; String Strzippath= FilePath +Tmpfilename; Try{Zipoutputstream out=NewZipoutputstream (NewFileOutputStream (Strzippath)); //two files that need to be downloaded simultaneously result.txt, Source.txtFile[] File1 = {NewFile (filepath+ "Test1.txt"), NewFile (filepath+ "Test 2.docx") }; for(inti = 0; i < file1.length; i++) {FileInputStream fis=NewFileInputStream (File1[i]); Out.putnextentry (NewZipEntry (File1[i].getname ())); //set the character encoding within the compressed file, or it will become garbledOut.setencoding ("GBK"); intLen; //read the contents of the file to be downloaded and package it into a zip file while(len = fis.read (buffer)) > 0) {out.write (buffer,0, Len); } out.closeentry (); Fis.close (); } out.close (); This. Downfile (GetResponse (), tmpfilename); } Catch(Exception e) {log.error ("File Download Error", E); } return NULL; } /*** Get response *@return */ PrivateHttpServletResponse GetResponse () {returnServletactioncontext.getresponse (); } /*** File Download *@paramResponse *@paramStr*/ Private voidDownfile (httpservletresponse response, String str) {Try{String path= FilePath +str; File File=NewFile (path); if(File.exists ()) {InputStream ins=NewFileInputStream (path); Bufferedinputstream Bins=NewBufferedinputstream (INS);//put it inside the buffer stream .OutputStream outs = Response.getoutputstream ();//get file output io streamBufferedoutputstream bouts =NewBufferedoutputstream (outs); Response.setcontenttype ("Application/x-download");//set the type of response contentResponse.setheader ("Content-disposition", "Attachment;filename=" + urlencoder.encode (str, "UTF-8"));//Set Header information intBytesread = 0; byte[] buffer =New byte[8192]; //start transferring file streams to the network while((bytesread = bins.read (buffer, 0, 8192))! =-1) {bouts.write (buffer,0, Bytesread); } bouts.flush ();//Be sure to call the Flush () method hereIns.close (); Bins.close (); Outs.close (); Bouts.close (); } Else{response.sendredirect (".. /error.jsp "); } } Catch(IOException e) {log.error ("File Download Error", E); } } }
After testing in a Windows environment, use STRUTS2
- Ant-1.6.5.jar (1009.8 KB)
Java Download File