我正在创建一个汇总插件来导入页面(标题,正文)和文件资源(pdf和图片)。
我使用PageManager来保存页面并保存/创建页面非常简单但添加附件我发现很难理解如何操作,因为网上几乎没有任何信息我可以找到怎么做。
有没有人知道如何通过inputStream或byte[]
设置页面对象的附件?
page.setAttachments()
获取了Attachment对象的列表,但是如果我尝试创建一个附件对象,则它不会采用inputStream或字节数组。
PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager
Page page = pageManager.getPage(8290525l)
URL url = new URL("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg");
InputStream bufferIn = new BufferedInputStream(url.openStream());
任何帮助将不胜感激
由于
编辑:
PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager
ArrayList<AttachmentResource> attachResources = new ArrayList<AttachmentResource>()
def page = pageManager.getPage(8290525l)
URL url = new URL("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg");
InputStream bufferIn = new BufferedInputStream(url.openStream());
DefaultSaveContext context = new DefaultSaveContext()
context.setUpdateLastModifier(true)
AttachmentManager attachmentManager = ComponentLocator.getComponent(AttachmentManager) as AttachmentManager
Attachment attachment = new Attachment("2454.png", "image/png", bufferIn.getBytes().length, "", false)
attachmentManager.saveAttachment(attachment, null, bufferIn)
page.addAttachment(attachment)
pageManager.saveContentEntity(page, context)
答案 0 :(得分:0)
使用FileUploadManager上传数据并添加附件(看起来您实际上正在使用Groovy,所以我也会这样做):
PageManager pageManager = ComponentLocator.getComponent(PageManager) as PageManager
Page page = pageManager.getPage(8290525l)
URL url = new URL("https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg");
InputStream bufferIn = new BufferedInputStream(url.openStream());
FileUploadManager uploadManager = ComponentLocator.getComponent(FileUploadManager) as FileUploadManager
AttachmentResource attachment = new InputStreamAttachmentResource (bufferIn, "2454.png", "image/png", bufferIn.getBytes().length)
uploadManager.storeResource(attachment, page)