解决springmvc上传文件时服务端中文文件名乱码问题

spring mvc 内部如果 request.getCharacterEncoding() 为 null 就默认为 ISO-8859-1,因此编写一个filter显式设置请求编码:

package com.xzp;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter(filterName="SetCharacterEncodingFilter",urlPatterns="/*")
public class SetCharacterEncodingFilter implements Filter{

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        if (request.getCharacterEncoding() == null) {  
            request.setCharacterEncoding("UTF-8");  
        }  
        filterChain.doFilter(request, response);  
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        
    }

}

你可能感兴趣的:(解决springmvc上传文件时服务端中文文件名乱码问题)