javaweb中的乱码问题

1.Jsp页面要设置

Java代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2. pageEncoding="UTF-8"%> 
<%@ 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字段中的参数不起作用,我们通常通过下面的代码来完成编码转换:

Java代码 复制代码 收藏代码
  1. String paramValue = request.getParameter("paramName");   
  2. paramValue = new String(paramValue.trim().getBytes("ISO-8859-1"), charset);   
String paramValue = request.getParameter("paramName");  
paramValue = new String(paramValue.trim().getBytes("ISO-8859-1"), charset);  

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

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

Xml代码 复制代码 收藏代码
  1. <Connector port="8080" protocol="HTTP/1.1"  
  2. connectionTimeout="20000"  
  3. redirectPort="8443"  
  4. URIEncoding="UTF-8" 
  5. /> 

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

Java代码 复制代码 收藏代码
  1. <% 
  2. String path = getClass().getResource("/").getPath();//获得src的目录 
  3. InputStream in = new FileInputStream(path + "confg.properties");//创建文件输入流 
  4. Properties property = new Properties(); 
  5. property.load(in); 
  6. int count = Integer.parseInt(property.getProperty("count")); 
  7. %> 
<%
 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了,我们可以用

Java代码 复制代码 收藏代码
  1. new String(value.getBytes("ISO8859-1"), encoding); 
new String(value.getBytes("ISO8859-1"), encoding);

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

Java代码 复制代码 收藏代码
  1. String device = new String(property.getProperty("device" + i).getBytes("ISO8859-1"), "UTF-8") ; 
  2. out.print("<option value=\"" + device + "\">" + device + "</option>"); 
String device = new String(property.getProperty("device" + i).getBytes("ISO8859-1"), "UTF-8") ;
out.print("<option value=\"" + device + "\">" + device + "</option>");

写个工具转换类也可以

Java代码 复制代码 收藏代码
  1. import java.io.UnsupportedEncodingException; 
  2. import java.util.Properties; 
  3.  
  4. public class PropertiesUtil { 
  5.     /**
  6.      * 指定编码获取properties文件中的属性值(解决中文乱码问题)
  7.      * @param properties java.util.Properties
  8.      * @param key 属性key
  9.      * @return value
  10.      */ 
  11.     public static String getProperty(Properties properties, String key, 
  12.             String encoding) throws UnsupportedEncodingException { 
  13.         if (properties == null
  14.             return null
  15.  
  16.         // 如果此时value是中文,则应该是乱码 
  17.         String value = properties.getProperty(key); 
  18.         if (value == null
  19.             return null
  20.  
  21.         // 编码转换,从ISO8859-1转向指定编码 
  22.         value = new String(value.getBytes("ISO8859-1"), encoding); 
  23.         return value; 
  24.     } 
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中空格路径错误

Java代码 复制代码 收藏代码
  1. <% 
  2. String path = getClass().getResource("/").getPath(); 
  3. out.println(getClass().getResource("/").getPath()); 
  4. out.println(getClass().getClassLoader().getResource("/").getFile()); 
  5. InputStream in = new FileInputStream(path + "confg.properties"); 
  6. Properties property = new Properties(); 
  7. property.load(in); 
  8. int count = Integer.parseInt(property.getProperty("count")); 
  9. %> 
<%
 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放到没有空格的路径之下,运行正常起来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值