【问题标题】:Getting attachments from document从文档中获取附件
【发布时间】:2012-02-15 10:37:27
【问题描述】:

这就是我所需要的,没什么太花哨的: 我正在从已附加在文档中的文件创建一个 url,但该文档未打开。我有一个 xpage,我想在其中显示特定文档的附件。我该怎么做?

提前谢谢你。

【问题讨论】:

    标签: lotus lotus-domino xpages


    【解决方案1】:

    最简单的方法是使用@AttachmentNames(在视图列中)来获取文件的名称。然后您可以使用 db.nsf/0/unid/$file/[filename] 构造 url——这是经典的,不会在 XPiNC 中运行。还有另一种特定于 XPage 的 URL 语法(需要检查):

    http(s)://[yourserver]/[application.nsf]/xsp/.ibmmodres/domino/OpenAttachment/[application.nsf]/[UNID|/$File/[AttachmentName]?Open

    在这里阅读我的完整文章:http://www.wissel.net/blog/d6plinks/SHWL-86QKNM

    (包括 SSJS 示例)

    【讨论】:

    • 感谢您的回复。我正在尝试这样做(这里不可读)link 现在,我在另一个 Xpage 上有这个,它可以完美运行,但在另一个 Xpage 上不行!?
    【解决方案2】:

    我发现DominoDocument.AttachmentValueHolder.getHref() 可用于获取附加文件或图像的 URL,如果您有文档句柄的话。例如:

    <xp:image
       id="image1">
       <xp:this.url>
          <![CDATA[#{javascript:document1.getAttachmentList("Body").get(0).getHref()}]]>
       </xp:this.url>
    </xp:image>
    

    您需要通过遍历从getAttachmentList() 返回的元素来处理多个附件。

    【讨论】:

      【解决方案3】:

      如果你可以使用 Java(比如在 XPages 中)那么

          import com.ibm.xsp.extlib.util.ExtLibUtil;
          import lotus.domino.MIMEEntity;
          import lotus.domino.Document;
          import lotus.domino.Session;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.Serializable;
          import java.text.SimpleDateFormat;
          import java.util.ArrayList;
          import java.util.Date;
          import java.util.Vector;
          import lotus.domino.Database;
          import lotus.domino.DocumentCollection;
          import lotus.domino.EmbeddedObject;
          import lotus.domino.Item;
          import lotus.domino.MIMEHeader;
          import lotus.domino.NotesException;
          import lotus.domino.RichTextNavigator;
          import lotus.domino.RichTextItem;
          import lotus.domino.Stream;
          import lotus.domino.View;
          // ...
          private String fileSeparator = File.separator;
          private String tempPath = System.getProperty("java.io.tmpdir") + fileSeparator + "Temp" + fileSeparator;
          // ...
          private void saveFilesFromDoc(Document doc) throws NotesException {
          if (doc.hasEmbedded()) {
              RichTextItem body = null;
              try {
                  body = (RichTextItem) doc.getFirstItem("body");
              } catch (ClassCastException e) {
                  // save file from MIME (Rich text is converted to MIME)
                  MIMEEntity mime = doc.getMIMEEntity();
                  findMimeWithFile(mime);
                  return;
              }
              if (body != null) {
                  // save file from richtext
                  RichTextNavigator rtnav = body.createNavigator();
                  if (rtnav.findFirstElement(RichTextItem.RTELEM_TYPE_FILEATTACHMENT)) {
                      do {
                          EmbeddedObject att = (EmbeddedObject) rtnav.getElement();
                          String fileName = att.getSource();
                          fileName = notConflictFileName(fileName );
                          String path = tempPath + fileName ;
                          att.extractFile(path);
                      } while (rtnav.findNextElement());
                  }
              } else {
                  // ("BODY is NULL");
              }
          }
      

      从richtext中获取文件转换为Mime

      private void findMimeWithFile(MIMEEntity mime) {
          try {
              askMimeForFiles(mime, "");
              MIMEEntity child = mime.getFirstChildEntity();
              while (child != null) {
                  askMimeForFiles(child, "child");
                  // String encoding = "ISO-8859-2";
                  String c = child.getContentType();
                  MIMEEntity subChild = child.getFirstChildEntity();
                  askMimeForFiles(subChild, "subChild");
                  if ("multipart".equals(c)) {
                      while (subChild != null) {
                          askMimeForFiles(subChild, "subChild2");
                          // String sc = subChild.getContentType();
                          subChild = subChild.getNextSibling();
                      }
                  }
                  child = child.getNextSibling();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      

      找出 MIME 实体是否是文件附件(或某些文本)

      private void askMimeForFiles(MIMEEntity mime, String prefix) throws NotesException {
          if (mime != null) {
              boolean thisMimeHasFile = false;
              String fileName = "noname";
              Vector<MIMEHeader> headers = mime.getHeaderObjects();
              for (MIMEHeader header : headers) {
                  // (prefix + "-header: " + header.getHeaderName() + " :: " + header.getHeaderValAndParams());
                  if ("Content-Transfer-Encoding".equals(header.getHeaderName())) {
                      if ("binary".equals(header.getHeaderVal())) {
                          thisMimeHasFile = true;
                      }
                  }
                  if ("Content-Disposition".equals(header.getHeaderName())) {
                      String val = header.getHeaderValAndParams();
                      int odd = val.indexOf("filename=") + "filename=".length();
                      int doo = val.length();
                      fileName = val.substring(odd, doo);
                      this.fileNames.add(fileName);
                  }
              }
              if (thisMimeHasFile) {
                  safeFilesFromMIME(mime, fileName);
              }
          }
      }
      

      如果 MIME 是文件附件,则保存它

      private void safeFilesFromMIME(MIMEEntity mime, String fileName) throws NotesException {
          Session session = ExtLibUtil.getCurrentSession(); // or user variableResolver
          Stream stream = session.createStream();
          String pathname = tempPath + fileName;
          stream.open(pathname, "binary");
          mime.getContentAsBytes(stream);
          stream.close();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-15
        • 1970-01-01
        • 2018-05-17
        • 2014-10-19
        • 1970-01-01
        相关资源
        最近更新 更多