如何在网页中显示word文档?

时间:2014-04-08 10:41:05

标签: java html ms-word

当用户点击特定文件时,我试图在网页上显示所有类型的文件,如PDF,ppt,word文档,mp3,mp4。使用JSF和前端使用jQuery显示所有文件。我能够使用对象标签显示PDF,但同样不适用于word文档。谁能建议我如何在网页上显示word文档和ppt文件?

2 个答案:

答案 0 :(得分:1)

您可以使用Apache POI Microsoft Office文档转换为html,并将其显示在网页中,就像 Google文档一样。

答案 1 :(得分:-1)

如果您使用的是servlet,对于PDF,代码就是这样,

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream servletOutputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {

    // Public user guide
    String fileName = "MyFile.pdf";


    bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName));

    byte[] data = new byte[bufferedInputStream.available()];
    bufferedInputStream.read(data);

    response.setContentType("application/pdf");
    servletOutputStream = response.getOutputStream();
    bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
    bufferedOutputStream.write(data);

} catch (Exception e) {
    LOG.error(e);
} finally {

    if (bufferedInputStream != null) {
        bufferedInputStream.close();
    }

    if (bufferedOutputStream != null) {
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }

    if (servletOutputStream != null) {
        servletOutputStream.flush();
        servletOutputStream.close();
    }
}

}

换句话说就是改变

response.setContentType("application/doc");