0

I want to serve MS documents inline instead of providing it as download by an attachment. I defined application mime type according to document type but the client still tries to download it. Here is my code:

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

}

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ahmet DAL
  • 4,445
  • 9
  • 47
  • 71

3 Answers3

1

It is not possible to render a raw MS document inside a page, unless there is some client side plugin which allows this (similar to what Flash provides for videos and games). I am not aware of such a plugin though. You will probably need to convert the document to HTML, and write that back to the client.

Eric Levine
  • 13,536
  • 5
  • 49
  • 49
  • It is possible to render a MS document inside a page by using an object tag. But in this case there is no HTML Page. If content dispostion `inline` is used the client is expected to open the application for the mime-type and not to ask for a download location. If the mime type is unknown to the client the client may ask for a download location. – andih Apr 15 '12 at 18:28
0

I think there is a way to use the object HTML Tag to embed an MS-Word or Excel document into an HTML page but I don't know how portable that is, even on Windows, between the different Browsers. Also you would perhaps have to got for HTML Frames to keep it limited to a certain area on your page.

If that's not what you want you could either convert it yourself using some library or use an API like the ones from Scribd or Slideshare which allow you to upload documents - also in MS formats - and get back an embeddable snippet. Though then your data is on hosted by their service.

Johannes Wachter
  • 2,715
  • 17
  • 17
0

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

The data you write to the output stream may be incomplete because in.available() may be less than the filesize. 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 
          }
       }
    }

}
Community
  • 1
  • 1
andih
  • 5,570
  • 3
  • 26
  • 36
  • @Whispered here's another [similar problem](http://stackoverflow.com/questions/1395151/content-dispositionwhat-are-the-differences-between-inline-and-attachment). Disposition 'inline' is only a hint for the browsers. For example in Firefox the user can overwrite the behavior in Settings->Applications. Have you allready tried it with different browsers using their default settings? – andih Apr 16 '12 at 04:09