2010-08-10 3 views
1

그것은 니펫 아래 작동합니다 날 것으로 보인다,하지만 "mp.getBodyPart (1) .getContent을(). toString()"반환toString을 사용하여 javamail 첨부를 구문 분석 할 수없는 이유는 무엇입니까?

[email protected]

첨부 파일 내용 대신

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

뿐만 아니라 http://stackoverflow.com/questions/5628395/javamail-parsing-email-content-cant-seem-to-get-it-to-work-message-getcont/26142591#이보기 26142591 – NoNaMe

답변

3

단순히 BASE64DecoderStream 클래스가 사용자 지정 toString 정의를 제공하지 않음을 의미합니다. 기본 toString 정의는 클래스 이름 + '@'+ 해시 코드를 표시하는 것으로, 이는 사용자가 보는 것입니다.

스트림의 '콘텐츠'를 가져 오려면 read() 메소드를 사용해야합니다.

+0

감사합니다. 이제 작동합니다. – jacknad

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"; 
     } 
     ... 
관련 문제