Java处理csv文件编码格式导致中文乱码解决方案

Java处理CSV文件上传与Hive导入:编码转换与步骤

本次开发使用java 处理csv 文件上传到服务器本地,再由服务器本地经过Hdfs 导入Hive 表中,步骤如下:

一、CsvEncodingConverter方法

package com.ys.common.hadoop;

import java.io.*;
import java.nio.charset.StandardCharsets;
import org.mozilla.intl.chardet.nsDetector;
import org.springframework.web.multipart.MultipartFile;

/**
 * CsvEncodingConverter 类用于检测和转换 CSV 文件的编码格式。
 */
public class CsvEncodingConverter {

    /**
     * 检测给定 MultipartFile 中 CSV 文件的编码格式。
     *
     * @param file 要检测的 CSV 文件
     * @return CSV 文件的编码格式,如果无法确定,则返回 null
     * @throws RuntimeException 如果在检测文件编码时发生错误
     */
    public static String findEncoding(MultipartFile file) {
        try {
            return guessFileEncoding(file);
        } catch (IOException e) {
            throw new RuntimeException("Error while detecting file encoding", e);
        }
    }

    /**
     * 通过检测 MultipartFile 中的 CSV 文件来猜测其编码格式。
     *
     * @param file 要检测的 CSV 文件
     * @return CSV 文件的编码格式,如果无法确定,则返回 null
     * @throws IOException 如果在检测文件编码时发生错误
     */
    private static String guessFileEncoding(MultipartFile file) throws IOException {
        try (InputStream inputStream = file.getInputStream()) {
            nsDetector detector = new nsDetector();
            byte[] buf = new byte[1024];
            int len;
            boolean isAscii = false;

            while ((len = inputStream.read(buf, 0, buf.length)) != -1) {
                isAscii = detector.isAscii(buf, len);
                if (isAscii) {
                    return "ASCII";
                }
                detector.DoIt(buf, len, false);
            }

            detector.DataEnd();

            String[] probableCharsets = detector.getProbableCharsets();
            if (probableCharsets.length > 0 && !"nomatch".equals(probableCharsets[0])) {
                return probableCharsets[0];
            } else {
                // 如果无法识别编码,返回默认编码 gb18030
                return "gb18030";
            }
        } catch (IOException e) {
            throw new IOException("检测文件编码时出错", e);
        }
    }

    /**
     * 将给定 MultipartFile 中的 CSV 文件转换为 UTF-8 编码,并保存到指定路径。
     *
     * @param file            要转换的 CSV 文件
     * @param sourceEncoding  原文件的编码格式
     * @param newFilePath     转换后的文件保存路径
     * @throws RuntimeException 如果在转换和保存文件时发生错误
     */
    public static void convertAndSaveToFile(MultipartFile file, String sourceEncoding, String newFilePath) {
        try (InputStream inputStream = file.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, sourceEncoding));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFilePath), StandardCharsets.UTF_8))) {

            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }

        } catch (IOException e) {
            // 处理不支持的字符集异常,可以选择使用默认字符集或其他支持的字符集
            e.printStackTrace();
        }
    }
}

二、方法调用

// 检测文件编码
String currentEncoding = CsvEncodingConverter.findEncoding(file);
logger.warn("currentEncoding:" + currentEncoding);
// 上传文件到本地服务器
CsvEncodingConverter.convertAndSaveToFile(file, currentEncoding, uploadFilePath);

参考网址:https://blog.csdn.net/qq_33505051/article/details/86736039

先检查csv文件的编码格式,如果获取失败,则默认读取文件编码格式为:gb18030b,再转化为UTF-8 编码格式,关于其他导入到Hive代码,有问题欢迎留言讨论。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值