2012-03-30 4 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() {} 

}; 

내 SocketServer의 코드를 게시하지 않습니다하지만 기본적으로 그냥 연결을 관리하고 함께 BufferedReader로 구현된다

01,234,536,413 : 나는 또한 캐릭터 세트를 지정하지 않고 시도

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

그래서 그 코드를 위해 그것을이다 :

은 여기 내 소켓 클라이언트입니다. 지금

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: ÚÞ´ 

내 SocketServer 클래스의 캐릭터 세트의 UTF-8을 지정할 때 이제 명령 프롬프트에서 내 결과가있다 내가 desesperate, 나는 적어도 30 시간 동안이 버그를 해결하는 시도하고, 내가 인터넷에서 발견 된 모든 솔루션을 시도했지만 그들 중 누구도 일하지 :(

, 나는 도움이 필요하세요!

고맙습니다. Raphael

답변

3

Windows 콘솔은 UTF-8 또는 ISO-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! –

관련 문제