zoukankan      html  css  js  c++  java
  • web中显示中文名称的图片,可以这样配置filter

    com.cy.filter.UrlFilter:

    package com.cy.filter;
    
    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)){                                //如果url中没有中文的话
                chain.doFilter(req, res);
                return;
            }
            
            //url中如果含有中文
            ch = ch.substring(request.getContextPath().length());
            config.getServletContext().getRequestDispatcher(ch).forward(req, res);
        }
    
        @Override
        public void destroy() {
            this.config = null;
        }
    
    }

    web.xml中的配置:

    <!-- 中文路径 -->
        <filter>
            <filter-name>urlFilter</filter-name>
            <filter-class>com.cy.filter.UrlFilter</filter-class>
            <init-param>
                <param-name>DEFAULT_URI_ENCODE</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>urlFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    index.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="resources/js/jquery.min.js"></script>
    <title>index</title>
    <style type="text/css">
    </style>
    </head>
    <body>
        <p>hello index</p>
        <div>
            <img src="resources/images/觅心射手.png" />
        </div>
        
    </body>
    <script type="text/javascript">
    
    </script>
    </html>

    访问index首页,可以正确的看到中文的图片。

  • 相关阅读:
    6 【程序6 最大公约数和最小公倍数】
    5 【程序5 判断分数等级】
    4 【程序4 分解质因数】
    3 【程序3 水仙花数】
    2【程序2 输出素数】
    1 【程序1 不死神兔】
    终极解决傻X阿里钱盾新手开店及老卖家复核身份证照片模糊无法对焦问题
    struct和typedef struct彻底明白了
    CentOS 6.5 编译安装 LNMP环境
    Another MySQL daemon already running with the same unix socket
  • 原文地址:https://www.cnblogs.com/tenWood/p/8563037.html
Copyright © 2011-2022 走看看