Package com.cn;import java.io.bufferedinputstream;import java.io.bufferedoutputstream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.outputstream;import java.net.urlencoder;import javax.servlet.servletexception;import javax.servlet.annotation.webservlet;import javax.servlet.http.httpservlet;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation class download */@WebServlet ("/download") public class Download extends httpservlet { private static final long serialVersionUID = 1L; /** * @see httpservlet#httpservlet () */ public download () { &nbSp;super (); // todo auto-generated constructor stub } /** * @see Httpservlet#doget (httpservletrequest request, httpservletresponse * response) */ protected void doget (httpservletrequest request, httpservletresponse response) throws ServletException, IOException { string name = "aaa.*";// file name string uploadpath = uploadfilehelper.getrepositorypath () + "//";// File source string filepath = name; string filename = name; //processing file name contains Chinese, browser garbled if ( Request.getheader ("User-agent"). toLowerCase (). IndexOf ("Firefox") > 0) { filename = new string (FileName.getBytes (" UTF-8 "), " iso8859-1 ");// firefox browser } else { if (Request.getHeader (" User-agent "). toUpperCase (). IndexOf (" MSIE ") > 0) { filename = urlencoder.encode (fileName, " UTF-8 ");// ie browser } } &nBsp; response.setcontenttype ("Text/plain"); response.setheader ("Location", filename); response.reset (); response.setheader ("Cache-Control", "max-age=0"); response.setheader ("Content-Disposition", "Attachment; filename=" + filename); bufferedinputstream bis = null; BufferedOutputStream bos = null; outputstream fos = null; InputStream fis = null; filepath = uploadpath + filepath; fis = new fileinputstream (FilePath); bis = new Bufferedinputstream (FIS); fos = Response.getoutputstream (); bos = new Bufferedoutputstream (FOS); int bytesread = 0; byte[] buffer = new byte[5 * 1024]; while ((bytesread = bis.read (buffer))  != -1) { bos.write ( Buffer, 0, bytesread);// send the file to the client } bos.close (); bis.close (); fos.close (); fis.close (); } /** * @see httpservlet#dopost (httpservletrequest request, Httpservletresponse * response) */ protected void dopost (HttpServletRequest request, httpservletresponse response) throws ServletException, IOException { Doget (request, response); }}
This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1694479
Server-side implementation file Download function code