File Download-the Chinese struts2 file is displayed as a space.

Source: Internet
Author: User

File Download-the Chinese struts2 file is displayed as a space.

 

Recently, file downloading is used in the company's projects. Based on the underlying layer, the file downloading mode of struts2 is determined. Most of the garbled characters solve the problem. When a Chinese file is downloaded, Chinese characters are displayed as spaces. After nearly three hours of searching various documents and meditation, I finally found the problem. I have not published any articles about this phenomenon on the Internet, so I will write it out and share it with you.

 

In the struts2 configuration file:

 

<! -- Data File Download -->

<Actionname = "downloadtemplfile" class = "filedownloadaction">

<Result name = "success" type = "stream">

<Paramname = "contenttype"> text/plain </param>

<Paramname = "contenttype"> application/octet-stream; charset = ISO8859-1 </param>

<Paramname = "contentdisposition"> attachment; filename = "$ {downloadfilename}" </param>

<Paramname = "inputname"> inputstream </param>

<Paramname = "buffersize"> 1024 </param>

</Result>

</Action>

 

Java usage:

 

Publicstring getdownloadfilename (){

String filename1 = servletactioncontext. getrequest (). getparameter ("FILENAME ");

Try {

Filename1 = new string (filename1.getbytes ("ISO8859-1"), "UTF-8 ");

} Catch (unsupportedencodingexception E1 ){

E1.printstacktrace ();

}

String downfilename = filename1;

Try {

Downfilename = newstring (downfilename. getbytes (), "ISO8859-1 ");

} Catch (exception e ){

E. printstacktrace ();

}

System. Out. println ("downfilename =======================>" + downfilename );

Return downfilename;

}

Publicinputstream getinputstream () throws exception {

String filename1 = servletactioncontext. getrequest (). getparameter ("FILENAME ");

Stringname = new string (filename1.getbytes ("ISO8859-1"), "UTF-8 ");

System. Out. println ("name ============>" + name );

Inputstreamin = servletactioncontext. getservletcontext (). getresourceasstream ("/custmonitorfile/" + name );

System. Out. println (in );

Returnin;

}

@ Override

Publicstring execute () throws exception {

Return success;

}

 

Note:

Why is the Java source code transferred twice?

Answer:

// Transcoding is required for fields obtained from JSP get or post for the first time.

String filename1 = servletactioncontext. getrequest (). getparameter ("FILENAME ");

Filename1 = newstring (filename1.getbytes ("ISO8859-1"), "UTF-8 ");

 

// The second time you set the Chinese file name to be sent to the client in ISO8859-1 format.

String downfilename = filename1;

Downfilename = newstring (downfilename. getbytes (), "ISO8859-1 ");

 

 

Note: Make sure you understand the purpose, origin, and end of each transcoding task.


If you have good suggestions, leave a message or send it to the author's email address: [email protected]

File Download-the Chinese struts2 file is displayed as a space.

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.