2011-09-05 4 views
5

텍스트가 quoted-printables입니다. 당신이 진실 = 3Dbeauty, 그때 확실히 20 =
수학을 = 철학의 가장 아름다운 지점입니다 생각되면인용 가능한 문자를 인용 부호에서 문자로 디코딩하는 방법은 무엇입니까?

: 여기에 (A wikipedia article에서) 같은 텍스트의 예입니다.

나는 공간으로, 문자로 인코딩 된 양식을 디코딩 자바 클래스, 예를 들어, 대한 = 20을 찾고 있어요.

UPDATE : 엘리트 신사 덕분에, 내가 QuotedPrintableCodec를 사용할 필요가 있음을 알고

import org.apache.commons.codec.DecoderException; 
import org.apache.commons.codec.net.QuotedPrintableCodec; 
import org.junit.Test; 

public class QuotedPrintableCodecTest { 
private static final String TXT = "If you believe that truth=3Dbeauty, then surely=20=mathematics is the most beautiful branch of philosophy."; 

    @Test 
    public void processSimpleText() throws DecoderException 
    { 
     QuotedPrintableCodec.decodeQuotedPrintable(TXT.getBytes());   
    } 
} 

그러나 나는 다음과 같은 예외가 점점 계속 :

org.apache.commons.codec.DecoderException: Invalid URL encoding: not a valid digit (radix 16): 109 
    at org.apache.commons.codec.net.Utils.digit16(Utils.java:44) 
    at org.apache.commons.codec.net.QuotedPrintableCodec.decodeQuotedPrintable(QuotedPrintableCodec.java:186) 

내가 잘못하고있는 중이 야 무엇을?

는 업데이트 2 : 나는 this question @ SO을 발견하고 배울 수있는 약 MimeUtility :

출력이 아직 완벽하지 않습니다 그러나
import javax.mail.MessagingException; 
import javax.mail.internet.MimeUtility; 

public class QuotedPrintableCodecTest { 
    private static final String TXT = "If you believe that truth=3Dbeauty, then surely=20= mathematics is the most beautiful branch of philosophy."; 

    @Test 
    public void processSimpleText() throws MessagingException, IOException 
    { 
     InputStream is = new ByteArrayInputStream(TXT.getBytes()); 

      BufferedReader br = new BufferedReader (new InputStreamReader( MimeUtility.decode(is, "quoted-printable")));   
      StringWriter writer = new StringWriter(); 

      String line; 
      while((line = br.readLine()) != null) 
      { 
       writer.append(line); 
      } 
      System.out.println("INPUT: " + TXT); 
      System.out.println("OUTPUT: " + writer.toString());  
    } 
    } 

, 그것은 '='가 포함

지금
INPUT: If you believe that truth=3Dbeauty, then surely=20= mathematics is the most beautiful branch of philosophy. 
OUTPUT: If you believe that truth=beauty, then surely = mathematics is the most beautiful branch of philosophy. 

내가 무슨 일을하고있는 중이 야 ?

답변

8

Apache Commons CodecQuotedPrintableCodec 클래스는 RFC 1521 Quoted-Printable 섹션의 구현입니다.


업데이트, Wikipedia의 예제에서 소프트 줄 바꿈이 사용되므로 인용 된 인쇄 가능 문자열이 잘못되었습니다.

소프트 줄 바꿈 :

Rule #5 (Soft Line Breaks): The Quoted-Printable encoding REQUIRES 
     that encoded lines be no more than 76 characters long. If longer 
     lines are to be encoded with the Quoted-Printable encoding, 'soft' 
     line breaks must be used. An equal sign as the last character on a 
     encoded line indicates such a non-significant ('soft') line break 
     in the encoded text. Thus if the "raw" form of the line is a 
     single unencoded line that says: 

      Now's the time for all folk to come to the aid of 
      their country. 

     This can be represented, in the Quoted-Printable encoding, as 

      Now's the time = 
      for all folk to come= 
      to the aid of their country. 

     This provides a mechanism with which long lines are encoded in 
     such a way as to be restored by the user agent. The 76 character 
     limit does not count the trailing CRLF, but counts all other 
     characters, including any equal signs. 

그래서 텍스트는 다음과 같이해야한다 :

private static final String CRLF = "\r\n"; 
private static final String S = "If you believe that truth=3Dbeauty, then surely=20=" + CRLF + "mathematics is the most beautiful branch of philosophy."; 

Javadoc을 명확하게 상태 :

규칙 # 3, # 4, 그리고 quoted-printable 사양의 # 5는 아직 구현되지 않았습니다 전체 인용 - 인쇄 가능 사양 자체가 제공되지 않기 때문입니다 은 바이트 [] 지향 코덱 프레임 워크에 잘 들어 있습니다. 압축 가능 코덱 프레임 워크가 준비되면 코덱을 완료하십시오. 부분 형식으로 코덱을 제공하는 뒤에있는 동기는 예를 들어 Q 코덱과 같은 형식으로 인쇄 된 형식의 굵게 인쇄 할 줄이 필요하지 않은 응용 프로그램의 경우에는 이미 으로 유용 할 수 있습니다.

그리고 소프트 줄 바꿈을 지원하지 않으므로 Apache QuotedPrintableCodec의 경우 bug logged이 있습니다.

+0

답변을 주셔서 감사합니다. 불행히도 위키 피 디아 페이지에서 예제를 디코딩하려고하면 예외가 발생합니다. – Skarab

+1

@Skarab, 코드 및 예외 스택 추적을 표시하지 않으면 예외가 발생했기 때문에 내 대답이 좋지 않다고 생각할 수 없습니다. 당신은 인용 가능한 문자들을 해독하는 클래스를 물었습니다. 그리고 저는 그것을 보여 줬습니다. –

+0

@ 스카럽, 숙제 연습에 대한 글을 올렸습니다. 나는 지금 +1을 기대해야한다 :) –

관련 문제