Java - Lotus Notes - Get Attachment Name from Item

2019-07-16 14:44发布

I have a lotus.domino.Item instance.

item.getType() returns Item.ATTACHMENT (meaning the item represents and attachment)

When I System.output the item.getValueString() it writes out the FileName and the Content.

That is not good for me as would like to extract the FileName and Content separately. Checked the API and could not find proper method to extract these.

Or is there a way to split the content returned by item.getValueString() to get the FileName and Content?

1条回答
太酷不给撩
2楼-- · 2019-07-16 15:36

Get EmbeddedObject class instance(s) from document:

java.util.Vector embeddedObjects = notesDocument.getEmbeddedObjects();

or from a rich text item:

java.util.Vector embeddedObjects = richTextItem.getEmbeddedObjects();

EmbeddedObject contains the method: getSource(), use it to get the filename of the attachment type of embdedded object.

if (embeddedObject.getType() == EmbeddedObject.EMBED_ATTACHMENT) {
    String attachmentFileName = embeddedObject.getSource();
}

To get file contents, use extractFile() method of the EmbeddedObject class instance.

Below there's java code that demonstrates how to detach attachments (sample is taken from the official Domino Designer documentation):

import lotus.domino.*;
import java.util.Vector;
import java.util.Enumeration;
public class JavaAgent extends AgentBase {
  public void NotesMain() {
    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();
      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      DocumentCollection dc = db.getAllDocuments();
      Document doc = dc.getFirstDocument();
      boolean saveFlag = false;
      while (doc != null) {
        RichTextItem body = 
        (RichTextItem)doc.getFirstItem("Body");
        System.out.println(doc.getItemValueString("Subject"));
        Vector v = body.getEmbeddedObjects();
        Enumeration e = v.elements();
        while (e.hasMoreElements()) {
          EmbeddedObject eo = (EmbeddedObject)e.nextElement();
          if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) {
            eo.extractFile("c:\\extracts\\" + eo.getSource());
            eo.remove();
            saveFlag = true;
            }
        }
        if (saveFlag) {
          doc.save(true, true);
          saveFlag = false;
          }
        doc = dc.getNextDocument();
      }
    } catch(NotesException e) {
      System.out.println(e.id + " " + e.text);
      e.printStackTrace();
    }
  }
}
查看更多
登录 后发表回答