HTML section
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Insert Title here</title></Head><Body> <ahref= "http://localhost:8080/webdemojava/download1?filename=a.flv"class= "Downloadbtn">a.flv</a> <ahref= "Http://localhost:8080/webdemojava/download1?filename=a.jpg"class= "Downloadbtn">A.jpg</a> <ahref= "Http://localhost:8080/webdemojava/download1?filename=a.mp3"class= "Downloadbtn">A.mp3</a> <ahref= "Http://localhost:8080/webdemojava/download1?filename=a.mp4"class= "Downloadbtn">A.mp4</a> <ahref= "Http://localhost:8080/webdemojava/download1?filename=a.txt"class= "Downloadbtn">A.txt</a> <ahref= "Http://localhost:8080/webdemojava/download1?filename=a.z1ip"class= "Downloadbtn">A.z1ip</a></Body></HTML>
Java code
PackageAction;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.net.URLEncoder;Importjavax.servlet.ServletException;ImportJavax.servlet.ServletOutputStream;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.jasper.tagplugins.jstl.core.Out;ImportSun.misc.BASE64Encoder; Public classDownLoad1extendsHttpServlet {Private Static Final LongSerialversionuid = 1L; PublicDownLoad1 () {Super(); //TODO auto-generated Constructor stub } protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {String filename= Request.getparameter ("filename");//get the file name sent overfilename =NewString (Filename.getbytes ("iso8859-1"), "UTF-8");//address the file name is Chinese. The problem of garbled characters//get the User-agent in the request headerString Agent = Request.getheader ("User-agent"); //different coding according to different customers. To resolve the downloaded file No name in ChineseString Filenameencoder = ""; if(Agent.contains ("MSIE")) { //IE BrowserFilenameencoder = Urlencoder.encode (filename, "Utf-8"); Filenameencoder= Filenameencoder.replace ("+", "" "); } Else if(Agent.contains ("Firefox")) { //Firefox BrowserBase64encoder Base64encoder =NewBase64encoder (); Filenameencoder= "=?utf-8?" B? " + Base64encoder.encode (filename.getbytes ("utf-8") + "? ="; } Else { //Other BrowsersFilenameencoder = Urlencoder.encode (filename, "Utf-8"); } String Realpath= This. Getservletcontext (). Getrealpath ("/download/" +filename);//relative path Change absolute pathInputStream in =NULL; Try{ in=NewFileInputStream (Realpath);//read the file to be downloaded. And do the test file exists}Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } if(in = =NULL) {Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Append ("The file was not found"); return; } Servletoutputstream out=Response.getoutputstream (); Response.setcontenttype ( This. Getservletcontext (). GetMimeType (filename);//set the returned file typeResponse.setheader ("Content-disposition", "attachment;filename=" +filenameencoder);//tell the customer that the file opens in attachment type (download) intLen = 0; byte[] buffer =New byte[1024]; while(Len=in.read (buffer)) >0) {out.write (buffer,0, Len); } in.close (); Out.close (); } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
Java file download function to solve Chinese garbled characters