上传的图片如果是中文命名
比较重要的一点::上传的图片如果是中文命名,不会出现乱码但就是显示不出来---》
解决方法
1.写一个过滤器
package com.ks.util;
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;
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;
}
}
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);
}
public void destroy() {
config = null;
}
}
Web.xml
<filter>
<filter-name>urlFilter</filter-name>
<filter-class>com.ks.util.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>
2.可以试试修改页面编码为gbk或者utf-8,不行的话,再修改tomcat配置文件server.xml,在Connector 里面增加一句URIEncoding="UTF-8"
3.(转) FCKeditor2.4.1 For Java使用3(解决上传中文图片乱码的问题)
JavafckeditorTomcatMyeclipse.net
这个版本可以建立中文文件夹,但是上传中文名字的图片或Flash时,上传后变成了乱码,
解决方法需要修改核心jar包中的ConnectorServlet.class文件
1. 下载源码fckeditor-java-2.4.1-src.zip。http://jaist.dl.sourceforge.net/sourceforge/fckeditor/fckeditor-java-2.4.1-src.zip
2. 解压后找到ConnectorServlet.java文件(路径:fckeditor-java-2.4.1\java-core\src\main\java\net\fckeditor\connector)
3. 修改ConnectorServlet.java并编译(保存后MyEclipse自动编译)。
首先把fckeditor-java-core-2.4.1.jar粘贴到你项目的lib目录下(为了修改ConnectorServlet.java时的编译),其次在src下建立一个包net.fckeditor.connector把ConnectorServlet.java粘贴到该包下,打开该类找到
Java代码
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
紧跟在下面加上
Java代码
- upload.setHeaderEncoding("UTF-8");
upload.setHeaderEncoding("UTF-8");
修改后为:
Java代码
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setHeaderEncoding("UTF-8");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
4. 找到编译好的class文件。
修改后保存,找你发布到Tomcat中的项目,并找到编译后的ConnectorServlet.class文件
5. 重新设置jar包。
找到原核心包fckeditor-java-core-2.4.1.jar修改后缀名为zip,解压成文件夹后找到ConnectorServlet.class替换成刚才编译的ConnectorServlet.class,然后压缩解压的文件夹,注意要选择zip,不是rar,最后改后缀名为.jar
6. 替换原来的fckeditor-java-core-2.4.1.jar,结束。
PS:我的tomcat的server.xml中的一句代码
原:
Xml代码
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
现在是:(加了URIEconding="UTF-8")
Xml代码
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEconding="UTF-8"/>