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-downloadeddocx file? Is my piece of code correct?

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 PoletMay 3 '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?
–
sp00mMay 3 '12 at 12:43

SO isn't like all those other sites, 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.
–
MakotoMay 3 '12 at 13:19