2011-09-05 8 views
5

Ho un testo con quoted-printables. Ecco un esempio di un testo del genere (da un wikipedia article):Come decodificare i caratteri quotabili (da quotabili a un carattere)?

Se credi che la verità = 3Dbeauty, allora sicuramente = 20 =
la matematica è la più bella ramo della filosofia.

Sto cercando una classe Java, che decodificare la forma codificata di caratteri, per esempio, = 20 ad uno spazio.

UPDATE: Grazie a The Gentleman Elite, so che ho bisogno di usare 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());   
    } 
} 

Tuttavia continuo a ricevere la seguente eccezione:

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) 

Che cosa sto facendo di sbagliato?

UPDATE 2: Ho trovato this question @ SO e conoscere 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());  
    } 
    } 

Tuttavia l'uscita ancora non è perfetto, contiene '=':

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. 

Ora quello che sto facendo di sbagliato ?

risposta

8

Apache Commons CodecQuotedPrintableCodec classe fa è l'implementazione della sezione Quota stampabile di RFC 1521.


Update, La stringa quoted-printable è sbagliato, come dimostra l'esempio su Wikipedia utilizza interruzioni di soft-line.

interruzioni di soft-line:

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. 

Così il testo deve essere effettuato come segue:

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."; 

Il Javadoc afferma chiaramente:

Regole # 3, # 4, e # 5 delle specifiche stampabili quotate non sono implementate ancora perché la specifica completa stampabile quoted non si presta bene nel framework di codec orientato byte []. Completare il codec una volta il framework codec vaporizzabile è pronto. La motivazione alla base di che fornisce il codec in una forma parziale è che può già essere disponibile in a portata di mano per quelle applicazioni che non richiedono la formattazione linea quotata- (regole # 3, # 4, # 5), ad esempio Q codec.

E c'è un bug logged per Apache QuotedPrintableCodec in quanto non supporta le interruzioni di linea.

+0

Grazie per la risposta, purtroppo ricevo un'eccezione quando provo a decodificare l'esempio dalla pagina di Wikipedia. – Skarab

+1

@Skarab, a meno che tu non ci mostri il codice e la traccia dello stack di eccezioni, non puoi pensare che la mia risposta sia negativa perché ha generato un'eccezione. Hai chiesto una lezione che decodifichi i caratteri quotabili e te l'ho mostrato. –

+0

@Skarab, ho aggiornato il mio post al tuo esercizio di compiti a casa. Dovrei aspettarmi un +1 ora :) –

Problemi correlati