struts2 File Download Chinese garbled problem solution

Source: Internet
Author: User

Problem Description: 1. Front page JSP pages through. Pass the Chinese parameter, the action receives the garbled 2. When downloading files, Chinese files cannot be displayed.

Solution: 1. Check the information on the Internet through the JSP page UrlEncode can be resolved, but I transfer English parameters, bypassing the problem

2. In the beginning did not understand the meaning of ${} in Struts.xml, leading to the filename has not been obtained. In fact, this is struts automatically load the values in the stack, because I passed the id,filename, there is definitely no value. Later in the SetID method, add a value to the filename, the method may be a little strange, but the effect is achieved.

3. Download file name into Chinese, there is a lot of information on the Internet, through the setup charset=iso8859-1 successfully resolved.

Code:

Struts.xml

                <action name= "downloadaction" class= "com.dcj.action.downloadAction" >
    	             <result type= "Stream" >
			<param name= "ContentType" >application/octet-stream;charset=ISO8859-1</param>
			<param name= " InputName ">inputStream</param>
			<param name=" contentdisposition ">attachment;filename=" ${ Docfilename} "</param>
			<param name=" buffersize ">4096</param>
		     </result>
		</ Action>

Action

       public void SetId (String id) {      this.id = ID;      doc d = (doc) (Docserviceinter.findbyid (Doc.class, Integer.parseint (ID));     docfilename = D.getfilename ();       }
Public      String getdocfilename () throws unsupportedencodingexception {     servletactionconte  Xt.getresponse (). SetHeader ("CharSet", "iso8859-1");    Return     New String (Docfilename.getbytes (), "iso8859-1");       }

Public      InputStream getInputStream () {return      servletactioncontext.getservletcontext (). getresource    Asstream ("/upload/" +docfilename);      }



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.