UTF8兼容性

时间:2014-12-25 15:08:37

标签: 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 = 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();
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

关键部分是将String转换为byte 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

    解释为ISO-8859-1时,
  • Atlético
  • 解释为UTF-8时
  • Atlético

对我来说问题似乎是解释转换后的字符串的代码或程序,而不是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);
    }
}