我正在使用 Spring Boot REST API 上传 csv 文件 MultipartFile。org.apache.commons.csv 的CSVFormat 库用于格式化MultipartFile,CSVParser 用于解析并将迭代的记录存入MySql 数据库。
csvParser = CSVFormat.DEFAULT
.withDelimiter(separator)
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withHeader(CsvHeaders.class)
.parse(new InputStreamReader(csvFile.getInputStream()));
观察结果是,当使用 UTF-8 字符集上传 CSV 文件时,它运行良好。但是如果 CSV 文件是其他格式(ANSI 等),它会将德语和其他语言字符编码为一些随机符号。
示例 äößü 被编码为 ����
我尝试了以下方法来指定编码标准,它也不起作用。
csvParser = CSVFormat.DEFAULT
.withDelimiter(separator)
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withHeader(CsvHeaders.class)
.parse(new InputStreamReader(csvFile.getInputStream(), StandardCharsets.UTF_8));
你能给些建议么。非常感谢你。