PDF 文件显示为“text/html”在浏览器中

发布于 2024-12-11 02:56:42 字数 561 浏览 0 评论 0原文

上下文:Apache 位于 Weblogic 应用程序服务器前面。

用户可以通过我们的 J2EE 应用程序下载 PDF 文件。 仅使用 Weblogic,PDF 就可以在所有浏览器的插件中正确显示。

但对于 Apache,HTTP 响应标头中包含 Content-type "text/html" (例如我们在 Firebug 中看到的),这会导致 Web 浏览器将文件内联显示为文本。 我们通过 Java 代码在 HTTP 标头中发送的内容: 内容处置 -> PDF 的名称,以及 Expires -> 0

未设置内容类型(为了简化,假设我们不知道应用程序中文件的类型),并且我们无法更改它。

看来 Apache 会覆盖它并设置自己的 Content-type,为什么? 我们尝试在 httpd.conf 中设置 DefaultType none 但没有效果。

当然,这对于所有文件类型都是一样的,PDF只是一个例子。

有什么想法吗?

Context : Apache in front of a Weblogic application Server.

A user can download a PDF file through our J2EE application.
With Weblogic alone, the PDF is displayed correctly in the plugin in all browsers.

But with Apache, the HTTP response header has Content-type "text/html" in it (as we can see in Firebug for example), which leads the web browser to display the file inline as text.
What we send in the HTTP header from our Java code :
Content-Disposition -> name of the PDF, and Expires -> 0

No Content-type is set (to simplify, let's say we do not know the type of the file in our application), and it is not possible for us to change that.

It seems that Apache overrides that and sets its own Content-type, why ?
We tried to set DefaultType none in the httpd.conf but without effect.

Of course, this is the same for all file types, PDF is only an example.

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘你却要生生世世 2024-12-18 02:56:42

我昨天遇到了同样的问题。就我而言,我发现当我(无意中)没有在 WebLogic 应用程序的 HTTP 响应上设置任何 Content-Type 值时,Apache 会“有帮助地”注意到这一点,并使用 设置 Content-Type 标头text/html——尽管我返回给客户端的内容是二进制 PDF。

这导致原始二进制 PDF 内容像纯文本一样显示在客户端浏览器中。

在 Apache 中也许可以抑制这种行为。然而,就我而言,我找到的解决方案是在提交响应之前在 HTTP 响应对象上设置 Content-Type。

(在我的应用程序(在 WebLogic 上运行的 Oracle ATG Commerce 应用程序)中,调用 flush() 后对 DynamoHttpServletResponse.setContentType()DynamoHttpServletResponse.setHeader() 的任何调用响应对象的 outputStream 上的 被默默地忽略。)

I ran into this same problem yesterday. In my case, I found that when I (inadvertently) wasn't setting any Content-Type value on the HTTP Response from my WebLogic application, Apache would "helpfully" notice this, and set a Content-Type header with of text/html -- even though the content I was returning to the client was a binary PDF.

This resulted in the raw binary PDF content being displayed in the client browser as if it were plain text.

It may be possible to suppress this behavior in Apache. However, in my case, the solution I landed on was to set a Content-Type on the HTTP response object before committing the response.

(In my application, an Oracle ATG Commerce app running on WebLogic, any calls to DynamoHttpServletResponse.setContentType() or DynamoHttpServletResponse.setHeader() after calling flush() on the response object's outputStream were being silently ignored.)

感情旳空白 2024-12-18 02:56:42

为什么你不尝试查找文件类型并填充内容类型。任何其他方式都是非标准的,并且可能会给您带来问题,具体取决于浏览器。

确定

InputStream is = new BufferedInputStream(new FileInputStream(file));
mimeType = URLConnection.guessContentTypeFromStream(is);

文件类型可以使用或

String fileName = "/path/to/file";
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();

// only by file name
String mimeType = mimeTypesMap.getContentType(fileName);

// or by actual File instance
File file = new File(fileName);
mimeType = mimeTypesMap.getContentType(file);

Why you don't try to find the filetype and fill the Content-type. Any other way would be non-standard and can give you problems depending on browser.

The filetypes can be determined using

InputStream is = new BufferedInputStream(new FileInputStream(file));
mimeType = URLConnection.guessContentTypeFromStream(is);

or

String fileName = "/path/to/file";
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();

// only by file name
String mimeType = mimeTypesMap.getContentType(fileName);

// or by actual File instance
File file = new File(fileName);
mimeType = mimeTypesMap.getContentType(file);
尸血腥色 2024-12-18 02:56:42

也许 apache 模块 mime_magic 有帮助吗?

如果没有,那么您可以将该文件保存在 www_root 内的某个目录中,并向浏览器发送重定向到该文件。这样,只有 apache 本身将文件发送回,并且使用 mime_magic 它也可以设置正确的内容类型。

Maybe the apache module mime_magic helps?

If it doesn't, then you could save the file in some directory inside www_root and send a redirect to the browser which leads to this file. That way, only apache itself sends the file back, and using mime_magic it can set the correct content-type too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文