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.