强制浏览器在JAVA中下载docx文件会生成损坏的文档 [英] Forcing the browser to download a docx file in JAVA generates a corrupted document

查看:718
本文介绍了强制浏览器在JAVA中下载docx文件会生成损坏的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JAVA,我正试图强制浏览器下载文件.

这是我当前使用的代码:

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

它几乎可以正常运行,但是在强制下载docx文档(MS Office 2007+)时,下载的文件已损坏(至少,这是MS Office告诉我的).如果我尝试直接在存储它们的服务器上打开它,则不会出现该错误,这意味着下载时(而不是上传时)确实会干扰该问题.

根据 IANA ,此类的MIME类型文件应为application/vnd.openxmlformats-officedocument.wordprocessingml.document (1),但指定MIME类型不能解决问题.

网络上有几条音轨,但是它们都不适合我.似乎有一个解决方案ASP.NET中,但在JAVA中找不到对应的内容.

我还尝试使用MIME类型application/vnd.ms-word (2),正如我看到的解决方案

我偶然尝试添加更多标头,实际上,Content-Length标头解决了问题...

所以最后,我只需添加以下行使其起作用:

response.setContentLength((int) file.length());

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?

解决方案

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

这篇关于强制浏览器在JAVA中下载docx文件会生成损坏的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆