`

JavaWeb中的乱码问题 Tomcate空格路径错误

阅读更多

1.Jsp页面要设置

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>

 这样才能写入中文字符。

 

2.request.setCharacterEncoding(charset);必须写在第一次使用request.getParameter()之前,这样才能保证参数是按照已经设置的字符编码来获取。
response.setCharacterEncoding(charset);必须写在PrintWriter out = request.getWriter()之前,这样才能保证out按照已经设置的字符编码来进行字符输出。

 

3.其中的2并不能解决所有的问题,对于post请求,无论是“获取参数环节”还是“输出环节"都是没问题的;对于get请求,"输出环节"没有问题,但是"获取参数环节"依然出现中文乱码,所以在输出时直接将乱码输出了。

原因是post请求和get请求存放参数位置是不同的:
post方式参数存放在请求数据包的消息体中。get方式参数存放在请求数据包的请求行的URI字段中,以?开始以param=value&parame2=value2的形式附加在URI字段之后。而request.setCharacterEncoding(charset); 只对消息体中的数据起作用,对于URI字段中的参数不起作用,我们通常通过下面的代码来完成编码转换:

String paramValue = request.getParameter("paramName");  
paramValue = new String(paramValue.trim().getBytes("ISO-8859-1"), charset);  

但是这样就必须每次都要修改,所以

解决方法:在tomcat_home\conf\server.xml 中的Connector元素中设置URIEncoding属性为合适的字符编码

<Connector port="8080" protocol="HTTP/1.1" 
connectionTimeout="20000" 
redirectPort="8443" 
URIEncoding="UTF-8"
/>

 

4.jsp页面中使用properties文件,注意properties文件在src的根目录下

<%
 String path = getClass().getResource("/").getPath();//获得src的目录
 InputStream in = new FileInputStream(path + "confg.properties");//创建文件输入流
 Properties property = new Properties();
 property.load(in);
 int count = Integer.parseInt(property.getProperty("count"));
%>

 

5.properties不能写中文这类的占两个字节的字符,否则回报错误,some character cannot be mapped using “ISO8859-1” character encoding的提示 ,这是因为我们的文件默认的保存格式是ISO8859-1的,而这种格式只能放西欧文字,放入中文等就会出现问题,即使放进去也会在读的时候出现乱码,所以我们要想解决问题,必须,首先修改文件保存类型为UTF-8,这个怎么修改不写了,还是罗嗦两句吧,在要修改的文件中按键alt+enter出来property窗口,修改Text file Encoding就好了。

这样这首我们在存储的时候存储正确,只要在读的时候读取正确就OK了,我们可以用

new String(value.getBytes("ISO8859-1"), encoding);

 其中value就是要修改的字符串了。

String device = new String(property.getProperty("device" + i).getBytes("ISO8859-1"), "UTF-8") ;
out.print("<option value=\"" + device + "\">" + device + "</option>");

 写个工具转换类也可以

import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class PropertiesUtil {
	/**
	 * 指定编码获取properties文件中的属性值(解决中文乱码问题)
	 * @param properties java.util.Properties
	 * @param key 属性key
	 * @return value 
	 */
	public static String getProperty(Properties properties, String key,
			String encoding) throws UnsupportedEncodingException {
		if (properties == null)
			return null;

		// 如果此时value是中文,则应该是乱码
		String value = properties.getProperty(key);
		if (value == null)
			return null;

		// 编码转换,从ISO8859-1转向指定编码
		value = new String(value.getBytes("ISO8859-1"), encoding);
		return value;
	}
}

如果你的应用创建中使用的系统默认编码,则如下转化:

PropertiesUtil.getProperty(properties, "TestKey", System.getProperty("file.encoding"));

 

6.Tomcate中空格路径错误

<%
 String path = getClass().getResource("/").getPath();
 out.println(getClass().getResource("/").getPath());
 out.println(getClass().getClassLoader().getResource("/").getFile());
 InputStream in = new FileInputStream(path + "confg.properties");
 Properties property = new Properties();
 property.load(in);
 int count = Integer.parseInt(property.getProperty("count"));
%>

 

输出的路径为下边

/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/CPE/WEB-INF/classes/

程序运行正常,但是一一打war包,放到Tomcate下就报错

同样输出路径为

/D:/Program%20Files/apache-tomcat-6.0.16/webapps/CPE/WEB-INF/classes/

愣是说

D:\Program%20Files\apache-tomcat-6.0.16\webapps\CPE\WEB-INF\classes\confg.properties 这个文件找不到。

解决方法,把Tomcate放到没有空格的路径之下,运行正常起来。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics