Java 如何以内联方式而不是作为附件提供MS office文档?

Java 如何以内联方式而不是作为附件提供MS office文档?,java,servlets,ms-office,mime-types,Java,Servlets,Ms Office,Mime Types,我希望以内联方式提供MS文档,而不是通过附件提供下载。我根据文档类型定义了应用程序mime类型,但客户端仍尝试下载它。这是我的密码: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream is = null; String fileName = "file.docx";

我希望以内联方式提供MS文档,而不是通过附件提供下载。我根据文档类型定义了应用程序mime类型,但客户端仍尝试下载它。这是我的密码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InputStream is = null;
    String fileName = "file.docx";

    try {
        java.io.File file = new java.io.File("C:/"+fileName);

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] bytes = new byte[in.available()];
        in.read(bytes);
        in.close();

        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.addHeader("Content-Disposition", "inline;filename=\"" + fileName + "\"");

        response.getOutputStream().write(bytes);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

这是如何造成的,我如何解决它?

除非有客户端插件允许,否则无法在页面中呈现原始MS文档(类似于Flash为视频和游戏提供的插件)。但我不知道有这样的插件。您可能需要将文档转换为HTML,然后将其写回客户端。

我认为有一种方法可以使用
对象
HTML标记将MS Word或Excel文档嵌入到HTML页面中,但我不知道它在不同浏览器之间的可移植性,即使在Windows上也是如此。此外,您可能还需要使用HTML框架来将其限制在页面的某个区域


如果这不是您想要的,您可以使用一些库自己转换它,或者使用类似的API,或者允许您上传文档(也是MS格式的文档)并获取可嵌入的代码片段。尽管如此,您的数据仍由他们的服务托管。

至少Internet Explorer允许用户访问。 您使用了哪些浏览器进行测试

写入输出流的数据可能不完整,因为.available()中的
可能小于文件大小。从InputStream的JavaDoc

返回可读取字节数的估计值(或 跳过)而不会被下一个 为此输入流调用方法

如果要读取整个块,请使用文件对象中的文件大小。 告诉客户文件的大小也是一个好主意

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    InputStream is = null;
    String fileName = "file.docx";
    BufferedInputStream in = null;

    try {
        java.io.File file = new java.io.File("C:/"+fileName);
        // not the best way
        int length = (int)file.length();

        in = new BufferedInputStream(new FileInputStream(file));
        // may allocate a huge ammout of memory
        byte[] bytes = new byte[length];
        in.read(bytes);
        in.close();
        in = null;

        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.addHeader("Content-Disposition", String.format("inline; filename=\"%s\" ; size=\"%d\"", fileName, length));

        response.getOutputStream().write(bytes);
        response.getOutputStream().flush();

    } catch (Exception e) {
        e.printStackTrace();
        throw new ServletException(e);
    } finally { 
       if (in != null) {
          try {
             in.close();
          } catch (IOException e) {
              e.printStackTrace();
              // ignore 
          }
       }
    }

}

可以使用对象标记在页面内呈现MS文档。但在本例中,没有HTML页面。如果使用了content-disposition
inline
,则客户端应打开mime类型的应用程序,而不是请求下载位置。如果客户端不知道mime类型,客户端可能会要求提供下载位置。@Whispersed这里是另一个。处置“内联”只是对浏览器的提示。例如,在Firefox中,用户可以覆盖设置->应用程序中的行为。您是否已准备好使用不同的浏览器使用其默认设置进行尝试?