Java UTF8兼容性

Java UTF8兼容性,java,utf-8,ftp,Java,Utf 8,Ftp,我正在使用一个函数通过FTP将一个文件上载到我的服务器。这是我的代码,工作正常,但创建的example.json文件与UTF8不兼容,因为它有Atlético而不是Atlético。有人能告诉我这有多正确吗?谢谢 public static void subir(){ String server = myserver; int port = 21; String user = mouser; String pass = mypas

我正在使用一个函数通过FTP将一个文件上载到我的服务器。这是我的代码,工作正常,但创建的example.json文件与UTF8不兼容,因为它有Atlético而不是Atlético。有人能告诉我这有多正确吗?谢谢

public static void subir(){
        String server = myserver;
        int port = 21;
        String user = mouser;
        String pass = mypass;

        FTPClient ftpClient = new FTPClient();
        try {

            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            // Uploads first file using an InputStream
            File firstLocalFile = new File("example.json");

            String firstRemoteFile = "MyDir/example.json";
            InputStream inputStream = new FileInputStream(firstLocalFile);

            System.out.println("Subiendo archivo a servidor...");
            boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
            inputStream.close();
            if (done) {
                System.out.println("Subido perfectamente");
            }


        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
为了保存我的文件,我使用

public static void guardar(){
        FileOutputStream fop = null;
        File file;
        String content = sBuffer.toString();

        try {

            file = new File("example.json");
            fop = new FileOutputStream(file);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            else{
                file.createNewFile();
            }

            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

            System.out.println("Archivo guardado");
            subir();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fop != null) {
                    fop.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

关键部分是将
字符串
转换为
字节序列
s

在你的情况下,这是一条线

byte[] contentInBytes = content.getBytes();
调用
String.getBytes()
时,它使用您的区域设置编码,根据您的观察,这似乎不是
UTF-8
。如果要使用特定编码,则需要指定编码。你可以用

byte[] contentInBytes = content.getBytes(StandardCharsets.UTF_8);
然而,在我看来,问题不在于如何将Java字符串转换为UTF-8,而在于如何解释UTF-8字符串

字节序列
41 74 6c c3 a9 74 69 63 6f

  • Atlético
    当解释为ISO-8859-1时
  • Atlético
    当解释为UTF-8时
对我来说,问题似乎在于解释转换字符串的代码或程序,而不是Java程序中的转换(不过,如果需要UTF-8,请修复它,使其不依赖于语言环境设置)

顺便说一下,如果要将文本(而不是二进制数据)保存到文件中,您可能需要选择
Writer
,而不是
OutputStream
。下面的方法演示如何使用
UTF-8
将字符串写入文件

import java.nio.charset.StandardCharsets;

public static void save(final File file, final String text) throws IOException {
    try (final OutputStream fout = new FileOutputStream(file);
        final Writer out = new OutputStreamWriter(fout, StandardCharsets.UTF_8)
    ) {
        out.write(text);
    }
}

如何将内容写入文件?您使用哪个类在文件中写入数据?然后您如何读取文件以查看Atlético?我已使用创建和保存文件的功能更新了我的问题该文件看起来不错,有AtlÃtico,问题在于上载我想谢谢,它说标准字符集不能解析为变量您使用什么Java版本<代码>标准字符集是在Java7中添加的。您是否
导入了java.nio.charset.StandardCharsets
?我正在使用Java6,没有其他方法可以做到这一点?您为什么要使用Java6?Java6是生命的终结。人们不应该再使用它了。看见您也可以使用
“UTF-8”
而不是
标准字符集.UTF_8
。请查看。谢谢,我已更改为Java 7,我已将代码更改为您的代码,但当我打开文件时,我仍在观看Atlético:(