SoFunction
Updated on 2025-03-08

jsp SmartUpload Chinese garbled problem solved

When using the jspsmartupload component to upload and download files, if the user chooses a file name with a Chinese name or the file path with a Chinese character, garbled code will appear. After a period of debugging, I have initially solved this problem. Now post the solved code.

1. Upload

In SmartUpload. In the Java file, add a property private String charset to perform character encoding conversion. There are two corresponding methods:

Copy the codeThe code is as follows:
public void setCharset(String charset)
{
    = charset;
}
public String getCharset()
{
    return ;
}

Two other changes:

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

Copy the codeThe code is as follows:
String s11 = new String(m_binArray,m_startData,(m_endData - m_startData) + 1,());

At this time, we should set it in the jsp that handles upload

Copy the codeThe code is as follows:
SmartUpload su = new SmartUpload();
("UTF-8");

That's it.

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

Copy the codeThe code is as follows:
String s;
try
{
    s = new String(m_binArray, i, (j - i) + 1,());
}
catch(Exception e)
{
    s = "";
}

In the file, add a property private String charset to perform character encoding conversion. There are two corresponding methods:

Copy the codeThe code is as follows:
public void setCharset(String charset)
{
    = charset;
}
public String getCharset()
{
    return ;
}

Another place needs to be changed
In the getContentString() method, change String s = new String(m_parent.m_binArray,m_startData,m_size); to

Copy the codeThe code is as follows:
String s;
try
{
    s = new String(m_parent.m_binArray,m_startData,m_size,());
}
catch(Exception e)
{
    s = "";
}

Regarding the file, I think it can be changed or not, and it will not have any impact on uploading.
After changing the source code in this way, it has good ability to solve the problem of Chinese garbled code.

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

Copy the 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((""))
        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);
    file = new (s);
    FileInputStream fileinputstream = new FileInputStream(file);
    long l = ();
    boolean flag = false;
    int k = 0;
    byte abyte0[] = new byte[i];
    if(s1 == null)
        m_response.setContentType("application/x-msdownload");
    else if(() == 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(() == 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 = (abyte0, 0, i);
        k += j;
        m_response.getOutputStream().write(abyte0, 0, j);
    }
    ();
}
 

In addition, it is necessary to add a method to obtain UTF-8 encoding of Chinese characters

Copy the codeThe code is as follows:
/**
* Convert the Chinese characters in the file name to a UTF8-encoded string so that the saved file name can be displayed correctly during downloading.
* Yu Yiqi 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<();i++) {
        char c = (i);
        if (c >= 0 && c <= 255) {
            (c);
        } else {
            byte[] b;
            try {
                b = (c).getBytes("utf-8");
            } catch (Exception ex) {
                (ex);
                b = new byte[0];
            }
            for (int j = 0; j < ; j++) {
                int k = b[j];
                if (k < 0) k += 256;
                ("%" + (k).toUpperCase());
            }
        }
    }
    return ();
}

Add this to SmartUpload. In the Java file, the problem of saving Chinese name garbled when downloading will not appear. After processing, I hope you can give you a reference and I hope you can support me more.