ServletResponse的outputstream不能写入以大写.PDF结尾的文件。

huangapple go评论79阅读模式
英文:

ServletResponse outputstream not writing the file with capital .PDF

问题

实际上,我正在尝试在Chrome中预览PDF并下载它。我正在将字节数组写入Servlet响应对象并设置以下标头。

response.setContentType("application/pdf; charset=utf-8");
response.setHeader("Content-Disposition", "inline; filename=" + fileName + ".PDF");

并在JSP中的动作目标设置为"_blank"。

当我点击下载按钮时,PDF文件会在新标签页中打开。但是当我尝试下载时,在Chrome和其他浏览器中(除了Firefox)显示"网络错误"。

实际文件格式类似于12345.PDF,是否存在.PDF和.pdf之间的contentType问题?

英文:

Actually I am trying to preview a pdf in chrome and download it. I am writing the byte array in the servlet response object and setting the following headers.

response.setContentType("application/pdf; charset=utf-8"); 
response.setHeader("Content-Disposition", "inline; filename="+fileName+".PDF");

and in jsp action target="_blank".

when I click download button. PDF file is open in new tab. But when I tries to download then it is showing "network error" in chrome and other browsers except firefox.

The actual file format is like 12345.PDF Is there any contentType issue occurs bw .PDF and .pdf

答案1

得分: 1

通常我使用Content-Disposition: attachment; filename="foo.bar"; 尝试添加"attachment; filename="+fileName+".pdf"

attachment表示资源可下载

还有一个已知类型的文件扩展名注册表:更多信息请参阅mozilla文档 [如何确定内容部分的正确MIME类型],所以pdf已注册,而PDF没有。

作为一种一般性的备用方案,您还可以尝试"application/octet-stream",将文件写成二进制流。

英文:

Normally I use Content-Disposition: attachment; filename="foo.bar"; Try adding "attachment; filename="+fileName+".pdf"

attachment indicates resource is downloadable

Also there is a file extensions registry of known types: more info here mozilla doc [How to determine the correct MIME type for your content section] so pdf is registered while PDF isn't.

As a general fallback you could also try "application/octet-stream" writing the file as a binary stream.

huangapple
  • 本文由 发表于 2020年8月12日 15:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63371982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定