Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploaded filename encoding issue for Japanese and Chinese

I am currently using the jQuery-File-Upload. I may upload some files with a Japanese or Chinese file name, and I can see that the file name is for example, "お疲れ様です.txt" or "测试文档.txt" in browser's debug mode, but in the backend(Java), they become "ã�Šç–²ã‚Œæ§˜ã�§ã�™.txt" and "测试文档.txt".
I once tried to set formAcceptCharset to UTF-8 but it does not work.
Question:
How to get the correct file name in Java side when parsing the MultipartFormData?

Thanks in advance.

BTW, The following is my data

-----------------------------25382434931419
Content-Disposition: form-data; name="file"; filename="�疲れ様��.txt"
Content-Type: text/plain
....

Add the Java codes
In fact I did nothing in Java side currently,

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String upload(InMultiPart inMP) {
    while (inMP.hasNext()) {
        InPart part = inMP.next();
        MultivaluedMap<String, String> headers = part.getHeaders();
        String fileName = null;
        if (!headers.containsKey("Content-Disposition")) {
            continue;
        } else {
            // get the file name here
            fileName = parseFileName(headers.getFirst("Content-Disposition"));
        }
        //.....
    }
    //......
}

private String parseFileName(String disposition) {
    int fileNameIndex = disposition.indexOf("filename=");
    if (fileNameIndex < 0) {
        return null;
    }
    int start = disposition.indexOf("\"", fileNameIndex) + 1;
    int end = disposition.indexOf("\"", start);
    return  disposition.substring(start, end);
}
like image 644
Edward Avatar asked Mar 12 '13 08:03

Edward


Video Answer


1 Answers

As Stephen C said a filter can be used to get the right encoding. We had this problem on JBOSS 7.1.1 and implemented a filter.

In web xml

<filter>
    <display-name>set character encoding</display-name>
    <filter-name>RequestEncodingFilter</filter-name>
    <filter-class>com.myapp.RequestEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>RequestEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Filter class:

public class RequestEncodingFilter implements Filter {

private static final String ENCODING = "encoding";
private String configuredEncoding;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    configuredEncoding = filterConfig.getInitParameter(ENCODING);
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    servletRequest.setCharacterEncoding(configuredEncoding);
    filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {
}

}

like image 102
Dan Avatar answered Oct 19 '22 19:10

Dan