Solve Chinese garbled jsp SmartUpload problem, jspsmartupload

Source: Internet
Author: User

Solve Chinese garbled jsp SmartUpload problem, jspsmartupload

When you use the jspsmartupload component to upload and download files, if you select a file name containing a Chinese name or the file path contains Chinese characters, garbled characters will appear. after a period of debugging, I have initially solved this problem. the solution code is now pasted out.

I. Upload

In the SmartUpload. java file, add a private String charset attribute for character encoding conversion. There are two corresponding methods:

Copy codeThe Code is as follows: public void setCharset (String charset)
{
This. charset = charset;
}
Public String getCharset ()
{
Return this. charset;
}

There are two other changes:

In the upload () method, change String s11 = new String (m_binArray, m_startData, (m_endData-m_startData) + 1)

Copy codeThe Code is as follows: String s11 = new String (m_binArray, m_startData, (m_endData-m_startData) + 1, this. getCharset ());

At this time, we should set it in the jsp for processing the upload.
Copy codeThe Code is as follows: SmartUpload su = new SmartUpload ();
Su. setCharset ("UTF-8 ");
You can.

In the getDataHeader () method, change String s = new String (m_binArray, I, (j-I) + 1 );

Copy codeThe Code is 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 private String charset attribute for character encoding conversion. There are two corresponding methods:

Copy codeThe Code is as follows: public void setCharset (String charset)
{
This. charset = charset;
}
Public String getCharset ()
{
Return this. charset;
}

You also need to change the location
In the getContentString () method, change String s = new String (m_parent.m_binArray, m_startData, m_size );

Copy codeThe Code is as follows: String s;
Try
{
S = new String (m_parent.m_binArray, m_startData, m_size, this. getCharset ());
}
Catch (Exception e)
{
S = "";
}

In the SmartFile. java file, I think it can be modified or not, and it will not affect the upload.
After such modification of the source code, the problem of Chinese garbled characters can be well solved.

Ii. Download
In the SmartUpload. java file, change the downloadFile (String s, String s1, String s2, int I) method

Copy codeThe Code is 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 method to get the UTF-8 encoding of Chinese characters.

Copy codeThe Code is as follows :/**
* Convert the Chinese characters in the file name into UTF-8 encoded strings so that another file name can be correctly displayed during download.
* Yu Yiqi 2003.08.01
* @ Param s original file name
* @ Return the reencoded file name
*/
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 ();
}

Add this to the SmartUpload. java file, and the problem of garbled Chinese names will not occur during the download. After processing, I hope to give you a reference, and I hope you can provide more support for the customer's house.

Related Article

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.