How do I get all the attachments from a .nsf(lotus notes) file using java

后端 未结 4 713
耶瑟儿~
耶瑟儿~ 2021-01-01 05:50

Steps followed :

Took a back of my lotus notes as sample.nsf

And then tried to read the attachments from the sample.nsf

Cod

4条回答
  •  长情又很酷
    2021-01-01 06:15

    To get all attachment from a notes document then i wrote this method(part of my class). This method takes the document and extract the attachment(Rich Text field)from Notes Document. This class also help you to know consider Example: In two document if there is same Attachment it extracts only one.

    Here just you have to set "filePath" where you have to extract your attachment.

    public boolean export(Document doc ) throws NotesException { 
    
    if (doc.hasEmbedded()) {
            Vector allItems;
            allItems = doc.getItems();
            HashSet attNames = new HashSet();
    
            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 atts = new HashSet();
                    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;
    }
    

提交回复
热议问题