WEB开发中的JAVA字符编码经验总结
一、概要
在JAVA应用程序特别是基于WEB的程序中,经常遇到字符的编码问题。为了防止出现乱码,首先需要了解JAVA是如何处理字符的,这样就可以有目的地在输入/输出环节中增加必要的转码。其次,由于各种服务器有不同的处理方式,还需要多做试验,确保使用中不出现乱码。
二、基本概念
2.1 JAVA中字符的表达
JAVA中有char、byte、String这几个概念。char 指的是一个UNICODE字符,为16位的整数。byte 是字节,字符串在网络传输或存储前需要转换为byte数组。在从网络接收或从存储设备读取后需要将byte数组转换成String。String是字符串,可以看成是由char组成的数组。String 和 char 为内存形式,byte是网络传输或存储的序列化形式。 举例:
英
String ying = “英”;
char ying = ying.charAt(0);
String yingHex = Integer.toHexString(ying);
82 F1
byte yingGBBytes = ying.getBytes(“GBK”);
GB编码的字节数值
D3 A2
2.2 编码方式的简介
String序列化成byte数组或反序列化时需要选择正确的编码方式。如果编码方式不正确,就会得到一些0x3F的值。常用的字符编码方式有ISO8859_1、GB2312、GBK、UTF-8/UTF-16/UTF-32。
ISO8859_1用来编码拉丁文,它由单字节(0-255)组成。
GB2312、GBK用来编码简体中文,它有单字节和双字节混合组成。最高位为1的字节和下一个字节构成一个汉字,最高位为0的字节是ASCII码。
UTF-8/UTF-16/UTF-32是国际标准UNICODE的编码方式。 用得最多的是UTF-8,主要是因为它在对拉丁文编码时节约空间。
UNICODE值 UTF-8编码
U-00000000 - U-0000007F: 0xxxxxxx
U-00000080 - U-000007FF: 110xxxxx 10xxxxxx
U-00000800 - U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
U-00010000 - U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-0020xx00 - U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 - U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
三、J2SE中相关的函数
String str =”英”;
//取得GB2312编码的字节
byte[] bytesGB2312 = str.getBytes(“GB2312”);
//取得平台缺省编码的字节(solaris为ISO8859_1,windows为GB2312)
byte[] bytesDefault = str.getBytes();
//用指定的编码将字节转换成字符串
String newStrGB = new String(bytesGB2312, “GB2312”);
//用平台缺省的编码将字节转换成字符串(solaris为ISO8859_1,windows为GB2312) String newStrDefault = new String(bytesDefault);
//用指定的编码从字节流里面读取字符
InputStream in = xxx;
InputStreamReader reader = InputStreamReader( in, “GB2312”);
char aChar = reader.read();
四、JSP、数据库的编码
4.1 JSP中的编码
(1) 静态声明:
CHARSET有两个作用:
JSP文件的编码方式:在读取JSP文件、生成JAVA类时,源JSP文件中汉字的编码 JSP输出流的编码方式:在执行JSP时,往response流里面写入数据的编码方式
(2) 动态改变:在往response流里面写数据前可以调用response.setContentType(),设定正确的编码类型。
(3) 在TOMCAT中,由Request.getParameter() 得到的参数,编码方式都是ISO8859_1。所以如果在浏览器输入框内输入一个汉字“英”,在服务器端就得到一个ISO8859_1编码的(0x00,0xD3,0x00,0xA2)。所以通常在接收参数时转码:
String wrongStr = response.getParameter(“name”);
String correctStr = new String(wrongStr.getBytes(“ISO8859_1”),”GB2312”); 在最新的SERVLET规范里面,也可以在获取参数之前执行如下代码:
request.setCharacterEncoding(“GB2312”);
4.2 数据库的编码
(1) 数据库使用UTF-16
如果String中是UNICODE字符,写入读出时不需要转码
(2) 数据库使用ISO8859_1
如果String中是UNICODE字符,写入读出时需要转码
写入:String newStr = new String(oldStr.getByte(“GB2312”), “ISO8859_1”); 读出:String newStr = new String(oldStr.getByte(“ISO8859_1”),”GB2312”);
五、源文件的编码
5.1 资源文件
资源文件的编码方式和编辑平台相关。在WINDOWS平台下编写的资源文件,以GB2312方式编码。在编译时需要转码,以确保在各个平台上的正确性:
native2ascii –encoding GB2312 source.properties
这样从资源文件中读出的就是正确的UNICODE字符串。
5.2 源文件
源文件的编码方式和编辑平台相关。在WINDOWS平台下开发的源文件,以GB2312方式编码。在编译的时候,需要指定源文件的编码方式:
javac –encoding GB2312
JAVA编译后生成的字节文件的编码为UTF-8。
①最新版TOMCAT4.1.18支持request.setCharacterEncoding(String enc)
②资源文件转码成company.name=\u82f1\u65af\u514b
③如果数据库使用utf-16则不需要这部分转码
④页面上应有
转码ⅰ:
String s = new String
(request.getParameter(“name”).getBytes(“ISO8859_1”),”GB2312”); 转码ⅱ:
String s = new String(name.getBytes(“GB2312”),”ISO8859_1”);
转码ⅲ:
String s = new String(name.getBytes(“ISO8859_1”),” GB2312”);
第二篇:java常用经验总结
一.判断某个IP是否存在:
package michael.net;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestPingCmd {
public static void main(String[] args) throws Exception { // 读取txt文件中的IP列表
TestPingCmd pinger = new TestPingCmd();
List<String> iplist = pinger
.getIpListFromTxt("d:/test/idc_ping_ip.txt"); // List<String> iplist = new ArrayList<String>();
// iplist.add("222.*.*.*");
// iplist.add("222.*.*.*");
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(50, 60, 60,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50),
new ThreadPoolExecutor.CallerRunsPolicy()); long startTime = System.currentTimeMillis();
final int maxCount = 4;
for (final String ip : iplist) {
executorPool.execute(new Runnable() {
public void run() {
TestPingCmd pinger = new TestPingCmd();
Integer countSucce = pinger.doPingCmd(ip, maxCount);
if (null != countSucce) {
System.out.println("host:[ " + ip + " ] ping cout: "
+ maxCount + " success: " + countSucc
e);
} else {
System.out
.println("host:[ " + ip + " ] ping cout null");
}
}
});
}
while (executorPool.getActiveCount() > 0) {
Thread.sleep(100);
}
System.out.println("complete ping jobs count = " + iplist.size()
+ " , total used time(ms) = "
+ (System.currentTimeMillis() - startTime)); executorPool.shutdown();
}
/**
* @param destIp
* @param maxCount
*/
public Integer doPingCmd(String destIp, int maxCount) { LineNumberReader input = null;
try {
String osName = System.getProperties().getProperty("os.name");
String pingCmd = null;
if (osName.startsWith("Windows")) {
pingCmd = "cmd /c ping -n {0} {1}";
pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
} else if (osName.startsWith("Linux")) {
pingCmd = "ping -c {0} {1}";
pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
} else {
System.out.println("not support OS");
return null;
}
Process process = Runtime.getRuntime().exec(pingCmd); InputStreamReader ir = new InputStreamReader(process .getInputStream());
input = new LineNumberReader(ir);
String line;
List<String> reponse = new ArrayList<String>(); while ((line = input.readLine()) != null) {
if (!"".equals(line)) {
reponse.add(line);
// System.out.println("====:" + line);
}
}
if (osName.startsWith("Windows")) {
return parseWindowsMsg(reponse, maxCount);
} else if (osName.startsWith("Linux")) {
return parseLinuxMsg(reponse, maxCount);
}
} catch (IOException e) {
System.out.println("IOException " + e.getMessage()); } finally {
if (null != input) {
try {
input.close();
} catch (IOException ex) {
System.out.println("close error:" + ex.getMessage
());
}
}
}
return null;
}
private int parseWindowsMsg(List<String> reponse, int total) { int countTrue = 0;
int countFalse = 0;
for (String str : reponse) {
if (str.startsWith("来自
") || str.startsWith("Reply from")) {
countTrue++;
}
if (str.startsWith("请求超时
") || str.startsWith("Request timed out")) {
countFalse++;
}
}
return countTrue;
}
private int parseLinuxMsg(List<String> reponse, int total) { int countTrue = 0;
for (String str : reponse) {
if (str.contains("bytes from") && str.contains("icmp_seq=")) {
countTrue++;
}
}
return countTrue;
}
/**
* @param filepath
*/
public List<String> getIpListFromTxt(String filepath) { BufferedReader br = null;
List<String> iplist = new ArrayList<String>();
try {
File file = new File(filepath);
br = new BufferedReader(new FileReader(file));
while (br.ready()) {
String line = br.readLine();
if (null != line && !"".equals(line)) {
iplist.add(line);
}
}
} catch (Exception e) {
e.printStackTrace(System.out);
} finally {
if (null != br) {
try {
br.close();
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
}
return iplist;
}
}
二.Dao层找不到有可能是:没有在action中set进去、没有在spring的application中注入dao、还有没有把路径给找好。
三.探索网站域名,连接是否已经无效即能否获得相应网站页面。
private void getHttpConnection(String dn,String proxy_ip,int proxy_port) throws Exception{
Map<String,Object> filterMap = new HashMap<String,Object>();
// 建立链接 java.net.URL url = new URL(dn); HttpURLConnection httpConnection = null; if (proxy_ip != null && proxy_ip.length() > 0 && !"无 // 设置代理 ".equals(proxy_ip) && proxy_port > 0) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy_ip, proxy_port));
// 连接指定的资源 httpConnection.connect(); // 判断连接是否成功 if (httpConnection.getResponseCode() !=
System.err.println("POST Error: " + httpConnection = (HttpURLConnection) url.openConnection(proxy); } else { } httpConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection.HTTP_OK) { //
} httpConnection.getResponseMessage()); }else{ } //连接成功