文件上传在linux下乱码问题

乱码原因:上传文件的编码如果不是UTF8,读取到前台,与浏览器设置的编码(一般为UTF8)不一致,导致乱码

解决方案:在上传文件后,拿到服务器上的文件,然后以它自身编码读取文件内容,再以UTF8形式写入原文件(覆盖)


/**

* 将文件内容读出来,并以UTF-8编码写入
*/
public static boolean writeUTF8ByFile(File file){
boolean flag = false;

String utf = txt2String(file);
OutputStreamWriter osw;
try {
osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
osw.write(utf);
osw.close();
flag = true;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

return flag;
}

/**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    private static String txt2String(File file){
        String result = "";
        try{
        // 如果文件的编码为windows-1252,则会产生乱码,需要重新设置为Unicode
String code = getFileCodeFormat(file);

logger.info("first code = " + code);

if(code.equals("windows-1252")) code = "Unicode";

 // 如果文件的编码为Big5,则会产生乱码,需要重新设置为GB18030

if(code.equals("Big5")) code = "GB18030";

logger.info("last code = " + code);

InputStreamReader isr = new InputStreamReader(new FileInputStream(
file), code);
            BufferedReader br = new BufferedReader(isr);//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                result = result + "\r\n" +s;
            }
            br.close();    
        }catch(Exception e){
        logger.error(e.getMessage());
        }
        result = result.trim();
        logger.info("file content : " + result);
        return result;
    }
    
    /**
* 判断文件的编码格式
* @param fileName :file
* @return 文件编码格式
* @throws Exception
*/
private static String getFileCodeFormat(File file) throws Exception{
/*------------------------------------------------------------------------ 
 detector是探测器,它把探测任务交给具体的探测实现类的实例完成。 
 cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 
 加进来,如ParsingDetector、 JChardetFacade、ASCIIDetector、UnicodeDetector。   
 detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的 
 字符集编码。 
--------------------------------------------------------------------------*/  


CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();  
/*------------------------------------------------------------------------- 
 ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于 
 指示是否显示探测过程的详细信息,为false不显示。 
---------------------------------------------------------------------------*/  
detector.add(new ParsingDetector(false));   
/*-------------------------------------------------------------------------- 
 JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 
 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以 
 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。 
---------------------------------------------------------------------------*/   
detector.add(JChardetFacade.getInstance());  
//ASCIIDetector用于ASCII编码测定  
detector.add(cpdetector.io.ASCIIDetector.getInstance());  
//UnicodeDetector用于Unicode家族编码的测定  
detector.add(cpdetector.io.UnicodeDetector.getInstance());  
java.nio.charset.Charset charset = null;  
try {  
     charset = detector.detectCodepage(file.toURL());  
} catch (Exception ex) {ex.printStackTrace();}  
if(charset!=null){  
   // System.out.println(file.getName()+"编码是:"+charset.name());  
}else { 
  // System.out.println(file.getName()+"未知");
}
return charset.name();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值