struts2 file download, depending on the browser, there will be different errors appear. Some in IE normal, but to the Firefox can not. The specific reasons are not yet known. The main problem is that in the case of downloading a file named Chinese, there is not much difference in English. The detailed code is given here
JSP page: <a href= "<s:url value= ' employee/test!downloadfile.action ' ><s:param name= ' fileName ' >ttt.doc </s:param> </s:url> > Download </a>
In action:
//File Download PublicInputStream Getdownloadfile ()throwsexception{System.out.println (GetFileName ()); InputStream InputStream= Servletactioncontext.getservletcontext (). getResourceAsStream ("/file/Liao Zheyi paper 2.doc"); System.out.println (InputStream); returnInputStream; } //Download PublicString DownloadFile ()throwsException {returnSUCCESS; } //definition file name PrivateString FileName; Public voidSetfilename (String name)throwsexception{
The noon name can be displayed normally under IE, but not in Firefox. Specific reasons unknown This. FileName =NewString (Name.getbytes ("iso8859-1"), "Utf-8"); } PublicString GetFileName () {return This. FileName; }
Struts.xml configuration file:
<Actionname= "Test"class= "Testaction"> <resultname= "SUCCESSF">/index.jsp</result> <resultname= "Success"type= "Stream"> <!--Specify the file download type application/octet-stream default value can download all types - <paramname= "ContentType">Application/octet-stream; </param> <!--Specify the file name and display of the download, but note the problem of garbled Chinese names, the solution is: to encode (different browsers will have different conditions) - <!--contentdisposition is how file downloads are handled, including inline (inline) and attachments (attachment), which are inline by default and configured when using attachments: Attac Hment;filename= "file name". - <paramname= "Contentdisposition">attachment;filename= "${filename}"</param> <!--obtained by the Getdownloadfile () method InputStream - <paramname= "InputName">DownloadFile</param> <!--Specify the cache size of the download file - <paramname= "BufferSize">2048</param> </result> </Action>
Struts File Download