标签:des style c class blog code
最近做一个SSH框架的项目,用tomcat发布,需要上传图片到指定路径,然后再将图片显示在页面上。有一个问题:如果是英文名称的图片,就正常显示,可如果是中文的,它就是显示不出来,于是乎,在网上各种百度,各种尝试,查出有两种解决方法:
方法一:
在tomcat下的server.xml文件中,添加编码格式如图:

但是这种方法是治标不治本,如果换一种服务器发布,照样会出现乱码问题,这时就需要采用第二种方法。
方法二:
定义一个中文过滤器,步骤如下:
第一步:创建过滤器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 |
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 EncodingFilter implements
Filter{ String encoding=null; FilterConfig filterConfig=null; //销毁方法 public
void destroy() { // TODO Auto-generated method stub this.encoding=null; this.filterConfig=null; } //过滤处理方法 public
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest req = (HttpServletRequest) request; String uri = req.getRequestURI(); String ch = URLDecoder.decode(uri, encoding); if(uri.equals(ch)) { chain.doFilter(req, response); return; } ch = ch.substring(req.getContextPath().length()); filterConfig.getServletContext().getRequestDispatcher(ch).forward(req, response); } //初始化方法 public
void init(FilterConfig filterConfig) throws
ServletException { this.filterConfig=filterConfig; this.encoding=filterConfig.getInitParameter("encoding"); }} |
第二步:在web.xml中配置此过滤器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
<!-- 中文编码过滤器 --><filter> <filter-name>encodingFilter</filter-name><!--过滤器的名字--> <filter-class>com.hdsx.gispf.filter.EncodingFilter</filter-class><!--过滤器文件所在位置--> <init-param> <param-name>encoding</param-name><!--初始参数名,指定jsp页面所用 编码集--> <param-value>UTF-8</param-value><!--初始参数值 ,指定中文编码集--> </init-param></filter><!-- 过滤器 映射路径配置 --><filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern><!-- 对所有目录进行中文过滤 --></filter-mapping> |
ok,大功告成,这样就可以正常显示中文名称的图片了。
关于SSH中tomcat下中文名称图片不显示的问题,布布扣,bubuko.com
标签:des style c class blog code
原文地址:http://www.cnblogs.com/ssrsblogs/p/3756539.html