日期:2014-05-17  浏览次数:20630 次

tomcat6 打开中文资源的问题
http://localhost:8080/ebook/中文说明.pdf

中文说明.pdf
在tomcat6的ebook的web目录下的,
也就是项目录 下的资源,
现在想直接点击链接打开,
但是因为中文名字的问题
因为文件名是由客户定义的所以这个问题还是会出现的,


------解决方案--------------------
添加一个过滤器试试看:

Java code
package com.test;

import java.io.IOException;
import java.net.URLDecoder;

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.http.HttpServletRequest;

public class UrlFilter implements Filter {

    public final static String DEFAULT_URI_ENCODE = "UTF-8";
    
    private FilterConfig config = null;
    private String encode = null;
    
    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
        this.encode = config.getInitParameter("DEFAULT_URI_ENCODE");
        if(this.encode == null) {
            this.encode = DEFAULT_URI_ENCODE;
        }
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        String uri = request.getRequestURI();
        String ch = URLDecoder.decode(uri, encode);
        if(uri.equals(ch)) {
            chain.doFilter(req, res);
            return;
        }
        ch = ch.substring(request.getContextPath().length());
        config.getServletContext().getRequestDispatcher(ch).forward(req, res);
    }    

    @Override
    public void destroy() {
        config = null;
    }
}