2011-10-21 101 views
3

步驟如下:我如何得到所有從.NSF附件(Lotus Notes)中的文件用java

拿了我的Lotus Notes一回作爲sample.nsf

,然後試圖讀取從sample.nsf

代碼的附件片段:

Database db = session.getDatabase("","C:\\Projects\\NotesToJava\\sample.nsf"); 
DocumentCollection dc = db.getAllDocuments(); 
Document doc = dc.getFirstDocument(); 

while (doc != null) { 
    RichTextItem body = (RichTextItem) doc.getFirstItem("Body"); 

    if (body.getEmbeddedObject("Request.xlsx") != null) 
     System.out.println("Found BPM_Dev_Access_Request.xlsx in " + doc.getItemValueString("Subject")); 

    doc = dc.getNextDocument(); 
} 

回答

0

您需要從每個文檔中獲取附件,而不是嵌入對象。事情是這樣的:

import java.util.Iterator; 

import lotus.domino.*; 

public final class DocAttachmentParser implements Iterator { 

private Session s; 
private Document doc; 
private Double count ; 
private Iterator attIterator = null; 
public Double getCount() { 
    return count; 
} 
public DocAttachmentParser(Session s, Document doc) throws NotesException { 
     this.s = s; 
     this.doc = doc; 
     if (s!=null && doc !=null){ 
      this.count = (Double) s.evaluate("@Attachments", doc).elementAt(0); 
      if (count.intValue() > 0){ 
       attIterator = s.evaluate("@AttachmentNames", doc).iterator(); 
       } 
     } 

} 
    public boolean hasNext() { 
     return count.intValue() > 0 ? attIterator.hasNext(): false; 
    } 

    public Object next() { 
     return count.intValue() > 0 ? attIterator.next(): null; 
    } 
    private String nextAttName(){ 
     return count.intValue() > 0 ? attIterator.next().toString(): null; 
    } 

    public void remove() { 
     if (count.intValue() > 0) attIterator.remove(); 
    } 

    public String getAll(){ 

     StringBuilder sb = new StringBuilder(); 
     if (count.intValue()>0){ 

      while (hasNext()) { 
       sb.append(nextAttName()); 
      } 
     } 

     return sb.toString(); 
    } 

} 
7

無需使用evaluate,仰望extractFile在Lotus Designer幫助

從Lotus幫助:

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(); 
    } 
    } 
} 
+0

由於一噸!!!! – user1007180

+1

@ user1007180所以,請接受這個答案正確的。這樣,像我這樣的未來讀者可以輕鬆找到解決方案。 ;) – bluish

1

要獲得從Notes文檔中的所有附件然後我寫了這個方法(我班的一部分)。 該方法以文件並提取從Notes文檔的附件(富文本字段)。這個類也幫助你知道考慮範例:在兩個文檔中,如果有相同的附件,它只提取一個。

這裏只是你必須設置「文件路徑」,你必須提取您的附件。

public boolean export(Document doc) throws NotesException { 

if (doc.hasEmbedded()) { 
     Vector<Item> allItems; 
     allItems = doc.getItems(); 
     HashSet<String> attNames = new HashSet<String>(); 

     for (int i = 0; i < allItems.size(); i++) { 
      Item item = allItems.get(i); 
      if (item.getType() == Item.RICHTEXT) { 
       RichTextItem riItem = (RichTextItem) item; 
       Vector emb = riItem.getEmbeddedObjects(); 
       String[] doublette = new String[emb.size()]; 
       Set<String> atts = new HashSet<String>(); 
       for (int j = 0; j < emb.size(); j++) { 
        EmbeddedObject embObj = (EmbeddedObject) emb.get(j); 
        if (!attNames.contains(embObj.getName())) { 
         attNames.add(embObj.getName()); 
         StringBuffer test = new StringBuffer(
           embObj.getSource()); 
         test.append('-'); 
         test.append(embObj.getName()); 
         test.append('-'); 
         test.append(embObj.getFileSize()); 
         String attDesc = test.toString(); 
         if (atts.contains(attDesc)) { 
          doublette[j] = attDesc; 
         } else { 
          doublette[j] = ""; 
          atts.add(attDesc); 
         } 

        } 

       } 

       for (int j = 0; j < emb.size(); j++) { 
        try { 
         EmbeddedObject embObj = (EmbeddedObject) emb.get(j); 
         String itemName = riItem.getName(); 
         bOk = extractFile(embObj, itemName); 
         embObj.recycle(); 
        } catch (NotesException e) { 
         bOk = false; 
         if (!"".equals(doublette[j])) { 
          bOk = true; 
          System.out.println(" duplicated attachment:") 
          log.append(doublette[j]); 

         } 
        } 
       } 
      } 
     } 



    } 
    return bOk; 
} 

    private boolean extractFile(EmbeddedObject embObj, String itemName) 
     throws NotesException { 
    boolean bOk = true; 
    if (embObj.getType() == EmbeddedObject.EMBED_ATTACHMENT) { 

      String fileName = embObj.getName();  
        String filePath = this.filesPath + fileName; 
        // Check if file already exists, then delete 
       if (FileUtils.killFile(filePath, false, true, true)) { 
         embObj.extractFile(filePath); 
        } else { 
         bOk = false; 
         System.out.println(", error in kill: " + filePath); 
        }   
    } 
    return bOk; 
} 
相關問題