总体框架使用jboss seam ,正常的在界面上输入中文,oracle存储是正常的,但是用程序写了一个批量导入,发现问题就出来了,不管数据文件格式用ASCII或者UTF8,导入到数据库里的都是乱码。怎么回事?
贴导入数据的代码:
package com.baida.beans;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.core.FacesMessages;
import org.jboss.seam.log.Log;
@Name("categoryImportor")
public class CategoryImportor {
@In(create=true) CategoryHome categoryHome;
@In FacesMessages facesMessages;
@Logger Log log;
private byte[] data;
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public void importFromFile()throws IOException{
if(data == null){
facesMessages.add("upload file has no content");
log.debug("upload file has no content");
return;
}
ByteArrayInputStream in = new ByteArrayInputStream(data);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
while(line != null){
parse(line);
line = reader.readLine();
}
}
private void parse(String line){
String[] ss = line.split(",");
String categoryNo = ss[0].trim();
String categoryName = ss[1].trim();
log.info("begin to parse category:"+categoryNo +"-"+categoryName);
Category parent = getParent(categoryNo);
Category category = new Category();
category.setCategory(parent);
category.setCategoryNo(categoryNo);
category.setCategoryName(categoryName);
categoryHome.setInstance(category);
categoryHome.persist();
}
private Category getParent(String categoryNo){
int index = categoryNo.lastIndexOf(".");
Category parent = null;
if(index > 0){
String parentNo = categoryNo.substring(0,index);
log.info("try to find parent no:"+parentNo);
parent = categoryHome.getCategoryByNo(parentNo);
if(parent == null){
}
}
return parent;
}
}
[b]问题补充:[/b]
我在这里有日志
log.info("begin to parse category:"+categoryNo +"-"+categoryName);
如果用ASCII格式的文本文件上传,日志里是正常的中文字符,UTf-8格式的文本就不行
我现在就试试
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));//假设你的使用的UTF-8编码。
[b]问题补充:[/b]
就是这个问题了,但是要改成GBK编码的
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"GBK"));
我的想法是:我的界面都是utf-8编码,传到服务器端,seam认为所有的都是UTF-8编码的,于是把文件内的内容也按照UTF-8编码来处理,所以出现了乱码。我告诉java这个文件是"GBK"的,所以就不会出现乱码了。
非常感谢!!!