将一个文档中的富文本和图像复制到另一个文档中的MIME

Per*_*ten 7 mime ckeditor xpages

我有一个解决方案,用于将富文本内容从一个文档复制到另一个文档中的MIME.请参见http://per.lausten.dk/blog/2012/12/xpages-dynamically-updating-rich-text-content-in-a-ckeditor.html.我在应用程序中使用它作为用户在新文档中插入内容模板的方式,并使内容在CKEditor中即时显示.

问题是内嵌图像不包含在复制中 - 只是对图像临时存储的引用.这意味着图像仅对当前会话中的当前用户可见.所以不是很有用.

我怎样才能包含图片?

2013年10月4日更新: 我仍在寻找解决方案.

Per*_*ten 5

我终于搞定了.它更简单,甚至不涉及MIME.诀窍是修改工作HTML中的图像标记以包含base64编码图像,以便src标记可以使用这种格式(这里以gif为例):

src="data:image/gif;base64,<base64 encoded image>"
Run Code Online (Sandbox Code Playgroud)

我已经有了从富文本字段中获取HTML所需的代码(请参阅我的问题中已经提到过的博客文章).所以我需要的是用正确的src格式替换图像src标签,包括base64编码的图像.

以下代码获取HTML并浏览每个包含的图像并修改src标记:

String html = this.document.getValue(fieldName).toString();
if (null != html) {
    final List<FileRowData> fileRowDataList = document.getEmbeddedImagesList(fieldName);
    if (null != fileRowDataList) {
        final Matcher matcher = imgRegExp.matcher(html);
        while (matcher.find()) {
            String src = matcher.group();
            final String srcToken = "src=\"";
            final int x = src.indexOf(srcToken);
            final int y = src.indexOf("\"", x + srcToken.length());
            final String srcText = src.substring(x + srcToken.length(), y);
            for (FileRowData fileRowData : fileRowDataList) {
                final String srcImage = fileRowData.getHref();
                final String cidImage = ((AttachmentValueHolder) fileRowData).getCID();
                final String typeImage = ((AttachmentValueHolder) fileRowData).getType();
                final String persistentName = ((AttachmentValueHolder) fileRowData).getPersistentName();

                // Add base 64 image inline (src="data:image/gif;base64,<name>")
                if (srcText.endsWith(srcImage)) {
                    final String newSrc = src.replace(srcText, "data:" + typeImage + ";base64," + getBase64(persistentName));
                    html = html.replace(src, newSrc);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是base64对图像进行编码的getBase64()方法:

private String getBase64(final String fileName) {
    String returnText = "";
    try {
        BASE64Encoder base64Enc = new BASE64Encoder();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        base64Enc.encode(this.getEmbeddedImageStream(fileName), output);
        returnText = output.toString();
    } catch (NotesException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return returnText;
}
Run Code Online (Sandbox Code Playgroud)

一些代码来自Tony McGuckinemailBean.