Solution of jsp SmartUpload Chinese Garbled Code Problem

  • 2021-09-16 07:42:42
  • OfStack

When uploading and downloading files with jspsmartupload component, if the user chooses a file name containing Chinese name or a file path containing Chinese, there will be garbled codes. After a period of debugging, I have preliminarily solved this problem. Now paste the solved code.

Step 1 Upload

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

public void setCharset(String charset)
{
    this.charset = charset;
}
public String getCharset()
{
    return this.charset;
}

Two other changes were made:

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

String s11 = new String(m_binArray,m_startData,(m_endData - m_startData) + 1,this.getCharset());

At this time, we should set it in jsp for processing upload

SmartUpload   su = new   SmartUpload();
su.setCharset("UTF-8");

Just do it.

In the getDataHeader () method, String s = new String (m_binArray, i, j-i) + 1); Replace with

String s;
try
{
    s = new String(m_binArray, i, (j - i) + 1,this.getCharset());
}
catch(Exception e)
{
    s = "";
}

In the SmartFile. java file, add an attribute private String charset for character encoding conversion, and there are two corresponding methods:

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); Replace with

String s;
try
{
    s = new String(m_parent.m_binArray,m_startData,m_size,this.getCharset());
}
catch(Exception e)
{
    s = "";
}

For SmartFile. java file, I think can change or not change, will not have any impact on upload.
After changing the source code in this way, it has a good ability to solve the problem of Chinese garbled codes.

Step 2 Download
In the SmartUpload. java file, change the downloadFile (String s, String s1, String s2, int i) method to read

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, it is necessary to add a method to obtain UTF-8 coding of Chinese characters

/**
* Convert the Chinese characters in the file name to UTF8 Coded string , So that the saved file name can be displayed correctly when downloading .
* Rain is also strange 2003.08.01
* @param s Original file name
* @return Recoded 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();
}


Related articles: