0

这是一些代码片段,可让您了解我到目前为止所获得的内容。我可以通过这种方式很好地输出 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();
4

1 回答 1

0

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

于 2010-05-20T13:32:55.873 回答