7

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

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

如何包含图像?

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

4

3 回答 3

5

我终于搞定了。它要简单得多,甚至不涉及 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();

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

下面是 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;
}

一些代码来自Tony McGuckin 的 emailBean

于 2013-10-11T22:32:56.100 回答
2

您能否通过 DominoDocument.AttachmentValueHolder 获得内联图像的句柄,请参阅http://public.dhe.ibm.com/software/dw/lotus/Domino-Designer/JavaDocs/XPagesExtAPI/8.5.2/com/ibm/ xsp/model/domino/wrapped/DominoDocument.AttachmentValueHolder.html

我在笔记文档中写了关于附件的博客,请参阅http://www.domino-weblog.nl/weblogs/Domino_Blog.nsf/dx/xpages-tip-get-easily-access-to-your-attachments-in-java.htm

于 2013-02-27T20:22:14.107 回答
1

可怕的 hack(您需要整理身份验证和服务器名称)

SSJS(从视图中获取源代码)

var unid = curRow.getUniversalID();
var body = getComponent("body1");
var magic = new demo.HTMLMagic();
magic.doMagic(database, unid, body);

爪哇

package demo;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import net.htmlparser.jericho.Attribute;
import net.htmlparser.jericho.Attributes;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.OutputDocument;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import lotus.domino.Database;
import lotus.domino.NotesException;

import com.ibm.misc.BASE64Encoder;
import com.ibm.xsp.component.xp.XspInputRichText;
import com.ibm.xsp.http.IMimeMultipart;
import com.ibm.xsp.model.domino.wrapped.DominoRichTextItem;

public class HTMLMagic {

private HttpClient          httpClient  = null;
private HttpHost            httpHost    = null;
//TODO: that needs to be resolved smarter
private static final String HOSTNAME    = "localhost";

public void doMagic(final Database database, final String unid, final XspInputRichText body) throws NotesException,
        ClientProtocolException, IOException {
    final String docURL = "http://" + HOSTNAME + "/__" + database.getReplicaID() + ".nsf/0/" + unid + "/Body?OpenField";
    final String fixedHTML = this.fixHTML(docURL);
    IMimeMultipart result = DominoRichTextItem.convertToMime("-- copied text--<br />" + fixedHTML);
    body.setValue(result);
}

private String fixHTML(final String rawHTMLstring) throws ClientProtocolException, IOException {
    HttpHost target = this.getHttpHost();
    HttpClient client = this.getHttpClient();
    HttpGet get = new HttpGet(rawHTMLstring);
    HttpResponse response = client.execute(target, get);
    InputStream data = response.getEntity().getContent();
    Source rawHTML = new Source(data);
    OutputDocument outputDocument = new OutputDocument(rawHTML);
    StringBuilder sb = new StringBuilder();
    String tagName = HTMLElementName.IMG;
    String attName = "src";
    List<StartTag> links = rawHTML.getAllStartTags(tagName);

    for (StartTag onelink : links) {
        String href = onelink.getAttributeValue(attName);
        if (href != null) {
            String replace = this.urltoData(href);
            if (replace != null) {
                sb.setLength(0);
                sb.append("<");
                sb.append(tagName);
                sb.append(" ");
                sb.append(attName);
                sb.append("=\"");
                sb.append(replace);
                sb.append("\"");
                Attributes atts = onelink.getAttributes();
                if (!atts.isEmpty()) {
                    for (int i = 0; i < atts.size(); i++) {
                        Attribute att = atts.get(i);
                        if (!att.getName().equals(attName)) {
                            sb.append(" ");
                            sb.append(att.getName());
                            sb.append("=\"");
                            sb.append(att.getValue());
                            sb.append("\" ");
                        }
                    }
                }
                sb.append(">");
                outputDocument.replace(onelink, sb.toString());
            }
        }
    }
    return outputDocument.toString();
}

private HttpClient getHttpClient() {

    if (this.httpClient == null) {

        // general setup
        SchemeRegistry supportedSchemes = new SchemeRegistry();

        // Register the "http" protocol scheme, it is required
        // by the default operator to look up socket factories.
        supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

        // prepare parameters
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setUseExpectContinue(params, true);

        ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, supportedSchemes);
        this.httpClient = new DefaultHttpClient(connMgr, params);
    }
    return this.httpClient;
}

private HttpHost getHttpHost() {
    if (this.httpHost == null) {

        this.httpHost = new HttpHost(HOSTNAME, 80, "http");
    }
    return this.httpHost;
}

private String urltoData(final String href) throws ClientProtocolException, IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("data:image/");
    sb.append(href.substring(href.lastIndexOf("FieldElemFormat=") + 1));
    sb.append(";base64,");

    // Here go the Image data
    HttpHost target = this.getHttpHost();
    HttpClient client = this.getHttpClient();
    HttpGet get = new HttpGet(href);
    HttpResponse response = client.execute(target, get);
    InputStream data = response.getEntity().getContent();

    BASE64Encoder encoder = new BASE64Encoder();
    OutputStream output = new ByteArrayOutputStream();
    encoder.encode(data, output);
    sb.append(output.toString());
    output.close();

    return sb.toString();
}

}

如果这对你有用,我会很好奇。Notes 客户端无法显示内联 HTML 图像

于 2013-10-08T14:07:37.673 回答