Xpages嵌入式对象开发:如何获取附件的文件日期
问题根因
Lotus Domino 提供的 EmbeddedObject 类无内置 getFileDate() 方法,该调用不存在是报错的直接原因。
解决方案
可通过附件文件名匹配父文档的对应条目,获取附件的存储日期,若需要原始文件日期可导出后读取IO属性,具体实现如下:
修正后代码
try { if (attachDoc.hasItem("Attachments")){ NotesRichTextItem attachment = attachDoc.getFirstItem("Attachments"); if (attachment != null) { java.util.Vector eos = attachment.getEmbeddedObjects(); if (eos.isEmpty()) { print("Attachments empty: no files do nothing"); } else { Enumeration e = eos.elements(); while (e.hasMoreElements()) { EmbeddedObject eoA = (EmbeddedObject) e.nextElement(); if(eoA.getFileSize() < 10000000){ String fileName = eoA.getName(); print(fileName); // 读取附件在Domino中的存储日期 NotesItem attachItem = attachDoc.getFirstItem(fileName); if (attachItem != null) { DateTime attachDate = attachItem.getDateTimeValue(); print(attachDate.toString()); // 回收Domino对象避免内存泄漏 attachDate.recycle(); attachItem.recycle(); } // 若需要获取附件原始创建/修改日期,取消注释以下代码,替换上面的日期读取逻辑 // String tempPath = "/temp/" + fileName; // 替换为你的临时目录路径 // eoA.extractFile(tempPath); // File tempFile = new File(tempPath); // long modifyTime = tempFile.lastModified(); // print(new Date(modifyTime).toString()); // tempFile.delete(); // 用完删除临时文件 }else{ print("Unable to send the email. The file size is " + Math.round(eoA.getFileSize()/1000000) + "MB."); } eoA.recycle(); } } } attachment.recycle(); } attachDoc.recycle(); }catch(Exception e){ print("error with attachment " + e.toString()); }
注意事项
- 上述代码中直接读取的
attachDate为附件上传到Domino系统后的存储时间 - 所有Domino原生对象使用完成后必须调用
recycle()方法回收,避免出现内存泄漏问题
内容的提问来源于stack exchange,提问作者Samuel Omopariola
相关产品推荐
相关产品推荐

