Struts File Download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.