iteye_18171 2008-07-22 12:17
浏览 316
已采纳

使用hibernate向oracle导入中文数据文件出现乱码

总体框架使用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"的,所以就不会出现乱码了。
非常感谢!!!

  • 写回答

1条回答 默认 最新

  • llade163 2008-07-22 14:32
    关注

    [code="java"]private void parse(String line){
    System.out.println(line);
    .....[/code]

    看看输出的是不是乱码?如果是乱码证明你的输入有问题:

    [code="java"]BufferedReader reader = new BufferedReader(new InputStreamReader(in)); [/code]
    需要改成:
    [code="java"]BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));//假设你的使用的UTF-8编码。[/code]

    如果不是乱码。那证明输出到数据库的过程编码有问题,在数据驱动属性上调试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮