Domanda

Mi sembra il frammento di seguito dovrebbe funzionare, ma "mp.getBodyPart (1) .getContent (). ToString ()" restituisce

  

com.sun.mail.util.BASE64DecoderStream@44b07df8

al posto del contenuto del l'allegato.

public class GMailParser {
    public String getParsedMessage(Message message) throws Exception {
        try {
            Multipart mp = (Multipart) message.getContent();
            String s = mp.getBodyPart(1).getContent().toString();
            if (s.contains("pattern 1")) {
                return "return 1";
            } else if (s.contains("pattern 2")) {
                return "return 2";
            }
            ...
È stato utile?

Soluzione

Questo significa semplicemente che la classe BASE64DecoderStream non fornisce una definizione toString personalizzato. La definizione di default toString è quello di visualizzare il nome della classe + '@' + codice hash, che è quello che si vede.

Per ottenere il "contenuto" della Corrente è necessario utilizzare il metodo read ().

Altri suggerimenti

questo Accessori analizza BASE64DecoderStream esattamente come necessario.

private String getParsedAttachment(BodyPart bp) throws Exception {
    InputStream is = null;
    ByteArrayOutputStream os = null;
    try {
        is = bp.getInputStream();
        os = new ByteArrayOutputStream(256);
        int c = 0;
        while ((c = is.read()) != -1) {
            os.write(c);
        }
        String s = os.toString(); 
        if (s.contains("pattern 1")) { 
            return "return 1"; 
        } else if (s.contains("pattern 2")) { 
            return "return 2"; 
        } 
        ... 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top