Uploaded filename encoding issue for Japanese and Chinese

后端 未结 1 1204
暖寄归人
暖寄归人 2021-02-09 02:40

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 \"测

相关标签:
1条回答
  • 2021-02-09 03:16

    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() {
    }
    

    }

    0 讨论(0)
提交回复
热议问题