1

Using JAVA, I'm trying to force the browser to download files.

Here is the code I currently use:

response.reset();
response.resetBuffer();
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
IOUtils.copy(in, out);

out.flush();
out.close();
in.close();
response.flushBuffer();

It works almost well, but when forcing the download of a docx document (MS Office 2007+), the downloaded file is corrupted (at least, that's what MS Office tells me). If I try to open it directly on the server which stores them, that error doesn't appear, which means that the problem does interfere when downloading (and not when uploading).

According to IANA, the MIME type of such a file should be application/vnd.openxmlformats-officedocument.wordprocessingml.document (1), but specifying that MIME type doesn't resolve the problem.

There are several tracks on the Web, but none of them worked for me. There seems to be a solution in ASP.NET, but I didn't find the equivalent in JAVA.

I also tried to use the MIME type application/vnd.ms-word (2) as I saw there, but the downloaded file is still corrupted. Idem for the MIME type application/msword (3) a guy suggested here, and for the generic MIME type application/octet-stream (4) as put forward on this forum.

Then, for each of these four MIME types, I tried to change the name of the downloaded file from myfile.docx to myfile.doc (no x anymore), but the problem persists.

So, how to force the download of an uncorrupted-when-downloaded docx file? Is my piece of code correct?

Community
  • 1
  • 1
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 1
    You must have something that does not work in the servlet returning the file because I have a WebApp that return docX for years and never had any problem downloading the file. You can always rename your *.docx in *.zip and see if the content of the zip file is corrupted or not. – Guillaume Polet May 03 '12 at 12:28
  • @GuillaumePolet I just tried renaming the file `myfile.zip`, uploading it and downloading it: the archive isn't corrupted. When re-renaming the downloaded file `myfile.docx`, the problem reappears. Which MIME type did you use? Did you renamed your file from `*.docx` to `*.doc`? – sp00m May 03 '12 at 12:43
  • Content type is set to 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' (without the single quotes). The files ends with .docx. Maybe consider posting your complete code of your servlet doPOst/doGet – Guillaume Polet May 03 '12 at 12:48

1 Answers1

3

I tried by chance to add more headers, and in fact, the Content-Length header resolved the problem...

So finally, I just add this line to make it work:

response.setContentLength((int) file.length());
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 1
    [SO isn't like all those other sites](http://meta.stackexchange.com/a/128554/175248), and it sometimes provokes people for saying that it's a forum. In either event, I'm glad to hear that you were able to resolve your own problem. Be sure to mark it solved in ~48 hours' time. – Makoto May 03 '12 at 13:19
  • @sp00m what was your content-type? – P-RAD Dec 18 '15 at 12:55
  • @P-RAD `application/vnd.openxmlformats-officedocument.wordprocessingml.document` – sp00m Dec 18 '15 at 13:04
  • @sp00m thanks for the info , Not able to get it worked . but adding a download attribute in markup worked !!! (http://www.w3.org/TR/html5/links.html) – P-RAD Dec 21 '15 at 11:19
  • 1
    @sp00m I could get it working because of the solution suggested by you. Tried a lot of suggestions listed in several stackoverflow questions. Nothing helped, but your answer did. I'm indebted to you, dear programmer! – coder May 12 '16 at 14:57