Problem description: Java app file download function, Chinese garbled characters in the output file name
Test environment: Tomcat and websphere6.1
Method 1:
Response.Setcontenttype("Application/X-download");
Filename = urldecoder.Decode(Filename,"UTF-8");
Filename = java.net. urlencoder.Encode(Filename,"UTF-8");
Response. setheader ("Content-disposition","Attachment; filename = \""+NewString (filename +". Zip"). Getbytes (),UTF-8") +"\"");
Method 2:
Response. setheader ("Content-disposition","Attachment; filename ="+Toutf8string(Filename));
Public StaticString toutf8string (string s ){
Stringbuffer sb =NewStringbuffer ();
For(IntI = 0; I <S. Length (); I ++ ){
CharC = S. charat (I );
If(C> = 0 & C <= 255 ){
SB. append (C );
}Else{
Byte[] B;
Try{
B = character.Tostring(C). getbytes ("UTF-8");
}Catch(Exception ex ){
Log. Error (Ex );
B =New Byte[0];
}
For(IntJ = 0; j <B.Length; J ++ ){
IntK = B [J];
If(K <0) K + = 256;
SB. append ("%"+ Integer.Tohexstring(K). touppercase ());
}
}
}
ReturnSB. tostring ();
}