2017-03-17 45 views
0

我需要识别附件是发件人的签名图像并忽略它,因为我想跳过那种附件,但我无法识别是否该特定的附件是签名图像。Java EWS-如何识别附件是否为发件人签名图像

或者用户可以在添加签名图像的同时添加自定义属性,以便我可以在程序中查找该属性?

if (emailMessage.getHasAttachments() || emailMessage.getAttachments().getItems().size() > 0) { 

//get all the attachments 
AttachmentCollection attachmentsCol = emailMessage.getAttachments(); 

log.info("File Count: " + attachmentsCol.getCount()); 

    Attachment attachment = attachmentsCol.getPropertyAtIndex(i); 
    //log.debug("Starting to process attachment "+ attachment.getName()); 

    //do we need to skip this attachment 

     FileAttachment fileAttachment = (FileAttachment) attachment; 
     // if we don't call this, the Content property may be null. 
     fileAttachment.load(); 
     booelan isSignatureImage = fileAttachment.isContactPhoto(); // this is false 
} 

}

回答

0

我想出了一个办法做到这一点。运行代码一次,确定要忽略的附件的散列,然后制作该散列的本地字符串。然后,处理每个附件,将其与本地散列字符串进行比较,如果它们匹配,则可以忽略它并转到下一个(如果您正在像我一样处理循环中的附件)。

  // you must first load your attachment to be able to call .getContent() on it 
      fileAttachment.load(); 

      // load content 
      byte[] b = fileAttachment.getContent(); 

      // get the hash of your attachment 
      byte[] hash = MessageDigest.getInstance("MD5").digest(b); 

      // after you run this on the attachment you do not want, take the string from 'actual' below and put it here 
      String expected = "YOUR HASHED STRING"; 

      String actual = DatatypeConverter.printHexBinary(hash); 
      System.out.println(actual); 

      // some conditional to check if your current hash matches expected 
      if(actual.equals(expected)){ 
       continue; 
      }