1

在我看来,下面的片段应该可以工作,但是“mp.getBodyPart(1).getContent().toString()”返回

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

而不是附件的内容。

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

2 回答 2

3

它只是意味着 BASE64DecoderStream 类不提供自定义 toString 定义。默认的 toString 定义是显示类名 + '@' + Hash Code,就是你看到的。

要获取 Stream 的“内容”,您需要使用 read() 方法。

于 2010-08-10T22:38:04.547 回答
1

这会根据需要完全解析 BASE64DecoderStream 附件。

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"; 
        } 
        ... 
于 2010-08-11T15:07:02.647 回答