When uploading and downloading files with jspsmartupload components, if the user chooses a filename containing the Chinese name or the file path contains Chinese, it will appear garbled. After a period of debugging, I have initially solved the problem. The resolved code is posted.
First, upload
In Smartupload. Java file, add a property private String charset is used for character encoding conversions, and there are two corresponding methods:
Copy Code code as follows:
public void Setcharset (String charset)
{
This.charset = CharSet;
}
Public String Getcharset ()
{
return this.charset;
}
In addition two places are changed:
In the upload () method, String s11 = new String (M_binarray,m_startdata, (m_enddata-m_startdata) + 1) is changed;
Copy Code code as follows:
String S11 = new String (M_binarray,m_startdata, (m_enddata-m_startdata) + 1,this.getcharset ());
This time we should set up in the JSP for processing uploads
Copy Code code as follows:
Smartupload su = new Smartupload ();
Su.setcharset ("UTF-8");
It's OK.
In the Getdataheader () method, String s = new String (M_binarray, I, (j-i) + 1) is changed;
Copy Code code as follows:
String s;
Try
{
s = new String (M_binarray, I, (j-i) + 1,this.getcharset ());
}
catch (Exception e)
{
s = "";
}
In the Smartfile.java file, add a property private String charset for character encoding conversions, with two methods:
Copy Code code as follows:
public void Setcharset (String charset)
{
This.charset = CharSet;
}
Public String Getcharset ()
{
return this.charset;
}
Another place needs to be changed.
In the Getcontentstring () method, String s = new string (m_parent.m_binarray,m_startdata,m_size);
Copy Code code as follows:
String s;
Try
{
s = new String (M_parent.m_binarray,m_startdata,m_size,this.getcharset ());
}
catch (Exception e)
{
s = "";
}
For the Smartfile.java file, I think that can be changed, do not have any impact on the upload.
After such changes in the source code, for the Chinese garbled problem has a very good ability to solve.
Second, download
In Smartupload. Java file, change the DownloadFile (string s, string s1, string s2, int i) method to
Copy Code code as follows:
public void DownloadFile (string s, string s1, string s2, int i)
Throws Servletexception, IOException, smartuploadexception
{
if (s = = null)
throw new IllegalArgumentException ("File" + S +
"' Not Found (1040).");
if (S.equals (""))
throw new IllegalArgumentException ("File" + S +
"' Not Found (1040).");
if (!isvirtual (s) && M_denyphysicalpath)
throw new SecurityException ("Physical path is
Denied (1035). ");
if (isvirtual (s))
s = M_application.getrealpath (s);
Java.io.File File = new Java.io.File (s);
FileInputStream FileInputStream = new FileInputStream (file);
Long L = file.length ();
Boolean flag = false;
int k = 0;
byte abyte0[] = new Byte[i];
if (S1 = null)
M_response.setcontenttype ("Application/x-msdownload");
else if (s1.length () = = 0)
M_response.setcontenttype ("Application/x-msdownload");
Else
M_response.setcontenttype (S1);
M_response.setcontentlength ((int) l);
m_contentdisposition = M_contentdisposition!= null? M_contentdisposition: "attachment;";
if (S2 = null)
M_response.setheader ("Content-disposition", M_contentdisposition + "filename=" + toutf8string (GetFileName (s)));
Else
if (s2.length () = = 0)
M_response.setheader ("Content-disposition", m_contentdisposition);
Else
M_response.setheader ("Content-disposition", M_contentdisposition + "filename=" + toutf8string (S2));
while ((long) K < L)
{
Int J = fileinputstream.read (abyte0, 0, I);
K = J;
M_response.getoutputstream (). Write (Abyte0, 0, J);
}
Fileinputstream.close ();
}
In addition, we need to add a UTF-8 encoding method to obtain Chinese characters.
Copy Code code as follows:
/**
* Converts the Chinese characters in the file name into UTF8 encoded strings so that the saved file names are displayed correctly when downloading.
* Rain is also strange 2003.08.01
* @param s original filename
* @return The file name after recoding
*/
public static string toutf8string (string s) {
StringBuffer sb = new StringBuffer ();
for (int i=0;i<s.length (); i++) {
char C = 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) {
System.out.println (ex);
b = new Byte[0];
}
for (int j = 0; J < B.length; J + +) {
int k = B[j];
if (k < 0) k = 256;
Sb.append ("%" + integer.tohexstring (k). toUpperCase ());
}
}
}
return sb.tostring ();
}
The
adds this to the smartupload. java files, the download of the other stored in Chinese name garbled problem will not appear, disposed of, hope to give you a reference, but also hope that we support the cloud-dwelling community.