1 protected voidDoPost (httpservletrequest request, httpservletresponse response)2 throwsservletexception, IOException {3String browser = "";4String fileName = "test. txt";6 Try {7Browser = Request.getheader ("User-agent");8 if( -1 < Browser.indexof ("MSIE 6.0") | |-1 < BROWSER.INDEXOF ("MSIE 7.0")) {9 //IE6, IE7 browserTenResponse.AddHeader ("Content-disposition", "Attachment;filename=" One+NewString (Filename.getbytes (), "Iso8859-1")); A}Else if( -1 < Browser.indexof ("MSIE 8.0")) { - //IE8 -Response.AddHeader ("Content-disposition", "Attachment;filename=" the+ Urlencoder.encode (fileName, "UTF-8")); -}Else if( -1 < Browser.indexof ("MSIE 9.0")) { - //IE9 -Response.AddHeader ("Content-disposition", "Attachment;filename=" ++ Urlencoder.encode (fileName, "UTF-8")); -}Else if( -1 < Browser.indexof ("Chrome")) { + //Google AResponse.AddHeader ("Content-disposition", at"Attachment;filename*=utf-8" + urlencoder.encode (filename, "UTF-8"))); -}Else if( -1 < Browser.indexof ("Safari")) { - //Apple -Response.AddHeader ("Content-disposition", "Attachment;filename=" -+NewString (Filename.getbytes (), "Iso8859-1")); -}Else { in //Firefox or other browsers -Response.AddHeader ("Content-disposition", to"Attachment;filename*=utf-8" + urlencoder.encode (filename, "UTF-8"))); + } -}Catch(Exception e) { the e.printstacktrace (); * } $InputStream in =NewFileInputStream ("D:\\test.txt");//get file input streamPanax Notoginseng intLen = 0; - byte[] buffer =New byte[1024]; theOutputStream out =Response.getoutputstream (); + while(len = in.read (buffer)) > 0) { AOut.write (Buffer,0,len);//output buffer data to the client browser the } + in.close (); - Out.flush (); $ out.close (); $}
Judge each browser, then decode and overweight
Java File Download Settings Chinese name (Response.AddHeader)