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

发布于 2024-08-23 04:27:49 字数 2045 浏览 4 评论 0原文

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

导出Servlet.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();

Here is some code snippet to give you an idea of what I got so far. I can output the Word document fine this way. I can also access the image via the URL in the browser, but the Word documents src does not appear to be hitting the servlet(according to some logs I have).

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

`

ExportImage.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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2024-08-30 04:27:49

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

Would another suitable approach be to generate the document completely on the server (with the image embedded already) and stream that to the requestor instead of trying to get the doc to do a separate https fetch? If so, Docmosis and JODReports can produce documents for you in doc format with images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文