2012-03-30 7 views
0

私は現在、このような等「àéèîï」だからRunnableをJARファイルに不正なエンコードが

としてフランス語の文字とメッセージを受信する必要があるソケットサーバを実行しようとしています、ここで契約です:私は私のソケットサーバーを実行したとき私が受け取るメッセージは、コンソールでアクセントを見ることができるので、適切なエンコーディングを持っています。しかし、ソケットサーバーを実行可能なjarファイルにエクスポートしてコマンドプロンプトで実行すると、受信したメッセージのエンコードが間違っているようです。

私はこの問題について多くの投稿があることを知っていますが、提示されたソリューションのどれも私のために働いていない、または何かが足りなくなっているかもしれません。ここで

はいくつかのコードです:私のソケットサーバーの場合 :

server = new SocketServer(port, SocketServer.ASCIIINPUT) { 

    @Override 
    public void processMessage(String message, Socket sender) throws MessageException { 
     try{ 
      System.out.println("Message without decoding : " + message); 
      System.out.println("Message with UTF-8 decoding : " + URLDecoder.decode(message, "UTF-8")); 
      System.out.println("Message with ISO-8859-1 decoding : " + URLDecoder.decode(message, "ISO-8859-1")); 
     } catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 

    @Override 
    public void socketIterationDone() {} 

}; 

、それは非常に長いですが、それは基本的には接続を管理し、とBufferedReaderのを実装しているので、私は私のSocketServerのコードを投稿しません

:私も文字セットを指定せずに試してみました

final BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8"))); 

:InputStreamReaderのは、このように受信したメッセージを読むことができるようにします

try { 
     Socket s = new Socket("127.0.0.1", 6005); 
     s.getOutputStream().write("With UTF-8 encoding: éèï\n".getBytes(Charset.forName("UTF-8"))); 
     s.getOutputStream().write("With ISO-8859-1 encoding: éèï\n".getBytes(Charset.forName("ISO-8859-1"))); 
     s.getOutputStream().write("Without encoding: éèï".getBytes()); 
     s.close(); 
    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

は、だから、コードのためにそれをです:

は、ここに私のソケットクライアントです。私は私のSocketServerクラスで文字セットUTF-8を指定するときに今、私の結果は、コマンドプロンプトにあります

C:\Users\nx_vostro_1\Desktop>java -jar test.jar 
Server listening on port: 6005 
Message without decoding : With UTF-8 encoding: ÚÞ´ 
Message with UTF-8 decoding : With UTF-8 encoding: ÚÞ´ 
Message with ISO-8859-1 decoding : With UTF-8 encoding: ÚÞ´ 
Message without decoding : With ISO-8859-1 encoding: ??? 
Message with UTF-8 decoding : With ISO-8859-1 encoding: ??? 
Message with ISO-8859-1 decoding : With ISO-8859-1 encoding: ??? 
Message without decoding : Without encoding: ?? 
Message with UTF-8 decoding : Without encoding: ?? 
Message with ISO-8859-1 decoding : Without encoding: ?? 

C:\Users\nx_vostro_1\Desktop>java -Dfile.encoding=UTF-8 -jar test.jar 
Server listening on port: 6005 
Message without decoding : With UTF-8 encoding: ├®├¿├» 
Message with UTF-8 decoding : With UTF-8 encoding: ├®├¿├» 
Message with ISO-8859-1 decoding : With UTF-8 encoding: ├®├¿├» 
Message without decoding : With ISO-8859-1 encoding: ´┐¢´┐¢´┐¢ 
Message with UTF-8 decoding : With ISO-8859-1 encoding: ´┐¢´┐¢´┐¢ 
Message with ISO-8859-1 decoding : With ISO-8859-1 encoding: ´┐¢´┐¢´┐¢ 
Message without decoding : Without encoding: ´┐¢´┐¢ 
Message with UTF-8 decoding : Without encoding: ´┐¢´┐¢ 
Message with ISO-8859-1 decoding : Without encoding: ´┐¢´┐¢ 

C:\Users\nx_vostro_1\Desktop>java -Dfile.encoding=ISO-8859-1 -jar test.jar 
Server listening on port: 6005 
Message without decoding : With UTF-8 encoding: ÚÞ´ 
Message with UTF-8 decoding : With UTF-8 encoding: ÚÞ´ 
Message with ISO-8859-1 decoding : With UTF-8 encoding: ÚÞ´ 
Message without decoding : With ISO-8859-1 encoding: ??? 
Message with UTF-8 decoding : With ISO-8859-1 encoding: ??? 
Message with ISO-8859-1 decoding : With ISO-8859-1 encoding: ??? 
Message without decoding : Without encoding: ?? 
Message with UTF-8 decoding : Without encoding: ?? 
Message with ISO-8859-1 decoding : Without encoding: ?? 

そして、私は私のSocketServerクラスで文字セットを指定しないとき:

C:\Users\nx_vostro_1\Desktop>java -jar test.jar 
Server listening on port: 6005 
Message without decoding : With UTF-8 encoding: ├®├¿├» 
Message with UTF-8 decoding : With UTF-8 encoding: ├®├¿├» 
Message with ISO-8859-1 decoding : With UTF-8 encoding: ├®├¿├» 
Message without decoding : With ISO-8859-1 encoding: ÚÞ´ 
Message with UTF-8 decoding : With ISO-8859-1 encoding: ÚÞ´ 
Message with ISO-8859-1 decoding : With ISO-8859-1 encoding: ÚÞ´ 
Message without decoding : Without encoding: ÚÞ´ 
Message with UTF-8 decoding : Without encoding: ÚÞ´ 
Message with ISO-8859-1 decoding : Without encoding: ÚÞ´ 

C:\Users\nx_vostro_1\Desktop>java -Dfile.encoding=UTF-8 -jar test.jar 
Server listening on port: 6005 
Message without decoding : With UTF-8 encoding: ├®├¿├» 
Message with UTF-8 decoding : With UTF-8 encoding: ├®├¿├» 
Message with ISO-8859-1 decoding : With UTF-8 encoding: ├®├¿├» 
Message without decoding : With ISO-8859-1 encoding: ´┐¢´┐¢´┐¢ 
Message with UTF-8 decoding : With ISO-8859-1 encoding: ´┐¢´┐¢´┐¢ 
Message with ISO-8859-1 decoding : With ISO-8859-1 encoding: ´┐¢´┐¢´┐¢ 
Message without decoding : Without encoding: ´┐¢´┐¢ 
Message with UTF-8 decoding : Without encoding: ´┐¢´┐¢ 
Message with ISO-8859-1 decoding : Without encoding: ´┐¢´┐¢ 

C:\Users\nx_vostro_1\Desktop>java -Dfile.encoding=ISO-8859-1 -jar test.jar 
Server listening on port: 6005 
Message without decoding : With UTF-8 encoding: ├®├¿├» 
Message with UTF-8 decoding : With UTF-8 encoding: ├®├¿├» 
Message with ISO-8859-1 decoding : With UTF-8 encoding: ├®├¿├» 
Message without decoding : With ISO-8859-1 encoding: ÚÞ´ 
Message with UTF-8 decoding : With ISO-8859-1 encoding: ÚÞ´ 
Message with ISO-8859-1 decoding : With ISO-8859-1 encoding: ÚÞ´ 
Message without decoding : Without encoding: ÚÞ´ 
Message with UTF-8 decoding : Without encoding: ÚÞ´ 
Message with ISO-8859-1 decoding : Without encoding: ÚÞ´ 

私はdesesperateだ、私は、少なくとも30時間、このバグを解決しようとした、私はインターネット上で見つかったすべてのソリューションを試みたが、それらのどれも働いていない:(

、私は助けを必要としてください!

は ラファエル

答えて

3

がWindowsコンソールがUTF-8ISO-8859-1どちらを使用していないことに注意してください、ありがとうございます。おそらくCP850を使用します。

あなたは、例えばCP850ÚÞ´としてデコードISO-8859-1とバイトE9 E8 EFとしてそのéèïエンコードを、表示されます。

私は、すべてをUTF-8と書いて、すべてをUTF-8と読んで、コンソールに表示されているものを信用するのではなく、UTF-8対応のエディタでテキストファイルに書き込んで開きます。

JavaコンパイラがJavaソースを編集しているエディタと同じエンコーディング(-encoding)を使用していることを確認してください。 I 強くUTF-8をお勧めします。

また、URLDecoder.decode(...)は、自分が行っているとは思わない。つまり、最高でも何もしていません。それはString.getBytes(...)の反対ではないからです。実際にURLでエンコードされたデータを送信していない場合は削除してください。

InputStreamReaderはすでにStringにバイトをデコードしています。対称の場合は、反対側にOutputStreamWriterを使用する必要があります。

は常に、常に常にあなたはエンコーディングを指定できるようにする方法のバージョンを使用してください。

  • エンコーディングを指定しないでString.getBytes()を使用しないでください。
  • エンコードを指定しないでnew String(byte[])を使用しないでください。
  • エンコードを指定しないでnew InputStreamReader(InputStream)を使用しないでください。
  • エンコードを指定しないでnew OutputStreamWriter(OutputStream)を使用しないでください。
  • など。

好ましくは、これらは失敗したエンコード/デコードに例外をスローするように構成することができるように、常に、CharsetEncoder又はCharsetDecoderを取るバージョンを使用します。

可能な限りどこでもエンコーディングを指定しないと、プラットフォームのデフォルトエンコーディングに依存します(基本的に、ランダム値がのグローバル変数)。

プラットフォームのデフォルトエンコードを誤って使用した場所は、あなたや他の誰かが別のプラットフォームまたは別の国のプログラムを試すまで、マニフェストを待つことがあるバグです。

+0

あなたは正しいです、私のデータは適切なエンコーディングを持っています、それは正しい出力エンコーディングを持っていないDOSウィンドウです。 ファイルにデータを書き込もうとしましたが、正しいエンコードでテキストが表示され、jarファイルを実行しているときにCP850を指定しようとしましたが、正しい文字が表示されていました。ありがとうChristoffer! –

関連する問題