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

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

本文介绍了将富文本和图像从一个文档复制到另一个文档中的MIME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案,用于将富文本内容从一个文档复制到另一个文档中的MIME。请参见。我在应用程序中使用它来作为用户在新文档中插入内容模板的方式,并让内容在CKEditor中即时显示。



问题是内联图像不包括在复制中 - 只是对图像的临时存储的引用。这意味着图像仅对当前会话中的当前用户可见。



更新2013年10月4日:
我仍​​在寻找这个解决方案。

解决方案

它更简单,甚至不涉及MIME。诀窍是修改工作HTML中的图像标签以包含base64编码图像,以便src标签可以使用此格式(此处以gif为例):

  src =data:image / gif; base64,< base64 encoded image> 

我已经有了从富文本字段获取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();

//添加base 64图像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);
}
}
}
}
}


$ b b

这里是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;
}

一些代码来自 。


I have a solution for copying rich text content from one document to MIME in another document. See http://per.lausten.dk/blog/2012/12/xpages-dynamically-updating-rich-text-content-in-a-ckeditor.html. I use this in an application as a way for the user to insert content templates in a new document and have the content appear on-the-fly in the CKEditor.

The problem is that inline images are not included in the copying - only a reference to temporary storage of the images. This means that the images are only visible for the current user in the current session. So not very useful.

How can I include images?

Update October 4, 2013:I'm still looking for a solution to this.

解决方案

I finally got it work. It was much more simple and did not even involve MIME. The trick was to modify the image tags in the working HTML to include the base64 encoded image so that the src tag could use this format (here shown with a gif as example):

I already had the code necessary to get the HTML from the rich text field (see my blog post already mentioned in my question). So all I needed was to replace the image src tags with the correct src format including the base64 encoded image.

The following code gets the HTML and goes through each of the included images and modifies the src tag:

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);
                }
            }
        }
    }
}

Here is the getBase64() method that base64 encodes an image:

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;
}

Some of the code is from the emailBean by Tony McGuckin.

这篇关于将富文本和图像从一个文档复制到另一个文档中的MIME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:43