2014-12-25 181 views
2

我正在使用一項功能通過FTP將一個文件上傳到我的服務器。這裏是我的代碼,工作正常,但創建的文件example.json不是UTF8兼容的,因爲它具有Atlético而不是Atlético。有人可以告訴我這是多麼正確?由於UTF8兼容性

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(); 
      } 
     } 
    } 
+2

你如何寫文件內容?你正在使用哪個類在你的文件中寫入數據? – varun

+2

然後你如何閱讀*文件以查看Atlético? –

+0

我已更新我的問題與創建和保存文件的功能 – Elena

回答

1

的關鍵部分是String轉換成byte個序列。

在你的情況,這是該行

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); 
    } 
} 
+0

謝謝,它說StandardCharsets不能解析爲變量 – Elena

+0

什麼Java你使用的版本是?在Java 7中添加了StandardCharsets。你有沒有導入java.nio.charset.StandardCharsets? –

+0

我正在使用Java 6,有沒有另外的方法來做到這一點? – Elena