English 中文(简体)
如何在线为管理事务办公室文件服务,而不是作为附件服务?
原标题:How to serve MS office documents inline instead of as attachment?

我想在线提供管理文件,而不是作为附件下载。 我根据文件类型界定了申请类型,但客户仍然试图下载。 我的守则是:

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文件,除非有某些客户的侧面图(类似于闪电提供的视频和游戏)。 我不知道这种gin。 很可能需要将该文件转换为超文本,并将它写回客户。

我认为有办法使用<条码>目标。 RUS Tag将MS-Word或Excel文件放在一个超文本的网页上,但我不知道,哪怕是视窗,这在不同的浏览器之间是怎样的。 或许你还必须用超文本胶片把它限制在你网页上某个地区。

如果不是你想要的,那么你要么利用一些图书馆,要么使用像磁带那样的图像,从Scribd,要么S滑坡共享,允许你上载文件,也用MS格式填写文件,并收回一个可移植的幻灯。 虽然如此,你的数据却由他们提供服务。

At least the Internet Explorer allows the user to overwrite the disposition. Which Browsers have you used for testing?

您在产出流上书写的数据可能不完整,因为<代码>>内可查()可能少于文档。 From the JavaDoc of InputStream

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

If you want to read the whole block use the File size from the file object. It s also a good idea to tell the client the size of the file.

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 
          }
       }
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签