java - 在 JAVA 中通过 SSL 生成嵌入图像的 Word 文档

标签 java ssl ms-word doc

这里是一些代码片段,可以让您了解我到目前为止得到了什么。我可以这样很好地输出Word文档。我也可以通过浏览器中的 URL 访问图像,但 Word 文档 src 似乎没有访问 servlet(根据我的一些日志)。

ExportServlet.java `

    response.setContentType("application/ms-word");

    String imageUrl = request.getScheme() + "://" + request.getServerName() +
                      ":" + request.getServerPort() + request.getContextPath() +
                      "/ExportImage";

    PrintWriter out = response.getWriter();

        out.println("<html xmlns:o='urn:schemas-microsoft-com:office:office'
    xmlns:w='urn:schemas-microsoft-com:office:word'
    xmlns:v='urn:schemas-microsoft-com:vml'
    xmlns='http://www.w3.org/TR/REC-html40'>
    <head>
            <title>Exported Documents</title>

            <!--[if gte mso 9]>
           <xml>
           <w:WordDocument>
           <w:View>Print</w:View>
           <w:Zoom>100</w:Zoom>
           <w:DoNotOptimizeForBrowser/>
           <w:BreakWrappedTables/>
           </w:WordDocument>
           </xml>
           <![endif]-->
    </head>
    <body>
    <img src=\"" + imageUrl + "\">
    </body>
</html>")
    out.flush();

`

导出图片.java

      Logger.log("getting Image");
      ServletContext servletContext = getServletContext();
        String filename = servletContext.getRealPath("myImage.gif");
        response.setContentType(
                servletContext.getMimeType(filename));
        File file = new File(filename);
        response.setContentLength((int)file.length());

        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();

        // Copy the contents of the file to the output stream
        byte[] buf = new byte[1024];
        int count = 0;
        while ((count = in.read(buf)) >= 0) {
            out.write(buf, 0, count);
        }
        in.close();
        out.flush();
        out.close();

最佳答案

另一种合适的方法是完全在服务器上生成文档(已经嵌入图像)并将其流式传输给请求者,而不是尝试让文档执行单独的 https 获取?如果是这样,DocmosisJODReports可以为您制作带图片的doc格式文档。

关于java - 在 JAVA 中通过 SSL 生成嵌入图像的 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2357242/

相关文章:

JAVA如何添加方法的输出

java - 用于 Web 应用程序的 JPF(Java 路径查找器)

apache - 子域和其他域的 SSL 证书

ssl - gitorious 拒绝使用 https 的新 bitnami 设置、亚马逊 ec2 实例的权限

ssl - Javamail 不适用于 gmail SMTP

java - 为什么在密文中翻转一点后我仍然可以解密部分数据?

c# - c#生成word文档

vba - 如何将Word 2013中格式化的段落复制到Excel?

ms-word - MS Word - 创建模板文本

java - OpenCV获取特定颜色范围内的像素