Struts会用文件上传组件帮我们获取文件名称。。并付给相应的Action中的属性。。我使用 String uploadFileName; 这个属性来获取上传的文件名。我上传的文件名称是 "泛型.txt"
上传后结果显示文件名称出现乱码。。 (这里我想在服务器也以“泛型.txt”这个名称来保存文件在磁盘上)
然后我直接把 uploadFileName 打印在后台上。System.out.println(uploadFileName); 输出 ????.txtSystem.out.println(uploadFileName.getBytes("UTF-8").length); 输出 16 System.out.println("????.txt".getBytes("UTF-8").length); 输出 8 做到这里 我无语了。。难道说 其中 对文件名 进行 URL 编码了??我想知道我如何才能正确的获得上传的文件名。。还有这个文件名 从 浏览器 到 服务器 倒地都经过了怎样的转换。。???请高手赐教
上传后结果显示文件名称出现乱码。。 (这里我想在服务器也以“泛型.txt”这个名称来保存文件在磁盘上)
然后我直接把 uploadFileName 打印在后台上。System.out.println(uploadFileName); 输出 ????.txtSystem.out.println(uploadFileName.getBytes("UTF-8").length); 输出 16 System.out.println("????.txt".getBytes("UTF-8").length); 输出 8 做到这里 我无语了。。难道说 其中 对文件名 进行 URL 编码了??我想知道我如何才能正确的获得上传的文件名。。还有这个文件名 从 浏览器 到 服务器 倒地都经过了怎样的转换。。???请高手赐教
只不过你在服务器端解析的时候没有使用合适的编码方式,所以出现乱码了。
也就是说,完全可以仅在服务器端做改动就OK了。
但是为了项目规范易维护,还是建议所有页面以及前后台都使用统一的编码格式。
可以用filter完成。
应该很全的了
用这个设置没是问题,我就是这样做的
用这个没什么问题,我就是这样用的。
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class SetCharEncode implements Filter
{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
String encoding = selectEncoding(request);
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request)
{
return (this.encoding);
}
}web.xml
<filter>
<filter-name>setchar</filter-name>
<filter-class>com.core.SetCharEncode</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>BIG5</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setchar</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这个配置 有什么功能 。 它具体都做了什么。。下面的这个我倒是知道怎么回事。。
new String("XXX.txt".getBytes("iso8859-1"),"utf-8");如果我只想通过自己 转换 来获取正确的 文件名 能做到吗?
加上 new String(uploadFileName.getBytes("iso8859-1"),"utf-8");结果还是显示 ????。txt
================================================
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
================================================
defalut.properties中:struts.i18n.encoding=UTF-8
===============================================
再加上上述两条试试吧!其它我真没办法了!呵呵
如果你把 用GBK 编码的字符 当成 utf-8 来转换成Java的字符串(也就是UNICODE字符集)然后你在 getBytes("utf-8"); new String( ... ,"GBK"); 是不可能在获得原来正确的字符了在转换中数据已经丢失了。。当然 要保证 不 乱码 最好还是 保持 编码 一致 就OK了。