简体   繁体   English

Java 错误地存储了西班牙语字符

[英]Java is store wrongly spanish characters

I have a Java app that is loading a csv file with some content that I need to import in a MySQL database.我有一个 Java 应用程序,它正在加载一个 csv 文件,其中包含我需要在 MySQL 数据库中导入的一些内容。

The issue is that for some spanish content like ñ or á é í ó ú, the app returns: EL ESPA OL问题是,对于一些西班牙语内容,如 ñ 或 á é í ó ú,应用程序返回: EL ESPA OL

The database Default Collection is latin1 - default collection I also try changing that to others like utf8, but the result was always the same.数据库默认集合是latin1 - default collection我也尝试将其更改为其他人,例如 utf8,但结果始终相同。

The way I import the file is:我导入文件的方式是:

BufferedReader br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(file)));
            System.out.println("*** Importing file **** " + file);
            try {
                String line;
                int i = 0;
                while ((line = br.readLine()) != null) {
                    final String[] parts = line.split(",");
                    if(parts != null && parts.length > 0 && !parts[0].equalsIgnoreCase("")){
                        System.out.println("Line: " + i++ + " Text: " + line);
...
...

And when I do the request to the backend I sent the following headers:当我向后端发出请求时,我发送了以下标头:

accept: application/json
accept-encoding: gzip, deflate, br
accept-language: es-419,es;q=0.9,en;q=0.8
content-type: application/json

Things to try :尝试的事情:

Recreate / Amend your table collations重新创建/修改您的表格整理

Default collation is just that - a default.默认排序规则就是这样 - 默认值。 Your new tables take whatever the current default is unless you specify a collation.除非您指定排序规则,否则您的新表采用当前默认值。

If you don't recreate or amend your existing tables, they'll remain encoded in latin1 .如果您不重新创建或修改现有表,它们将保持在latin1编码。

Check your JRE codepage检查您的 JRE 代码页

Are you running on Windows?你在 Windows 上运行吗? What's your JRE default codepage set to?您的 JRE 默认代码页设置为什么?

It's been a while since I touched Java but I used to have terrible trouble with multi-byte chars because I would develop on Linux, and transfer the code to Windows and ... suddenly, funky chars everywhere.我接触 Java 已经有一段时间了,但我曾经在多字节字符方面遇到过可怕的麻烦,因为我会在 Linux 上开发,然后将代码转移到 Windows 上……突然之间,到处都是时髦的 字符。

This may have changed at some point but on Linux and OSX, JREs start with the default encoding as UTF-8 .. and on Windows it uses the system code page.这可能在某些时候发生了变化,但在 Linux 和 OSX 上,JRE 以默认编码作为 UTF-8 .. 开始,而在 Windows 上它使用系统代码页。 Which for me was cp1252 - decidedly not international.这对我来说是 cp1252 - 绝对不是国际的。

Try printing this system property to the console to see if this is happening to you.尝试将此系统属性打印到控制台,看看您是否发生了这种情况。

System.getProperty("file.encoding")

You can change the value of this at startup with a -D argument as usual, eg您可以像往常一样在启动时使用-D参数更改 this 的值,例如

java -Dfile.encoding="UTF-8" -jar myJar.jar

... or, as @ControlAltDel says, always specify an encoding when you have the option in a stream API. ...或者,正如@ControlAltDel 所说,当您在流 API 中有选项时,始终指定编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM