Java webapp:添加内容处置标头以强制浏览器“另存为”行为 [英] Java webapp: adding a content-disposition header to force browsers "save as" behavior

查看:107
本文介绍了Java webapp:添加内容处置标头以强制浏览器“另存为”行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使它不是HTTP 1.1 / RFC2616 webapps的一部分,希望强制资源在浏览器中下载(而不是显示),也可以使用 Content-Disposition 这样的标题:

Even though it's not part of HTTP 1.1/RFC2616 webapps that wish to force a resource to be downloaded (rather than displayed) in a browser can use the Content-Disposition header like this:

Content-Disposition: attachment; filename=FILENAME

即使很难,它只在RFC2183和不是部分定义HTTP 1.1它可以在大多数Web浏览器中使用。

Even tough it's only defined in RFC2183 and not part of HTTP 1.1 it works in most web browsers as wanted.

所以从客户端来看,一切都足够好。

So from the client side, everything is good enough.

然而在服务器端,在我的情况下,我有一个Java webapp,我不知道我应该如何设置该标头,特别是在以下情况下......

However on the server-side, in my case, I've got a Java webapp and I don't know how I'm supposed to set that header, especially in the following case...

我将在Amazon S3实例上托管一个文件(称为 bigfile )(我的S3存储桶可以使用部分地址访问,例如: files.mycompany.com / )这样用户就可以在 files.mycompany.com/bigfile 访问此文件。

I'll have a file (say called "bigfile") hosted on an Amazon S3 instance (my S3 bucket shall be accessible using a partial address like: files.mycompany.com/) so users will be able to access this file at files.mycompany.com/bigfile.

现在有办法制作 servlet (或 .jsp ),以便 Content-Disposition 当用户想要下载该文件时,总是会添加标题?

Now is there a way to craft a servlet (or a .jsp) so that the Content-Disposition header is always added when the user wants to download that file?

代码会是什么样的,如果有的话会有什么问题?

What would the code look like and what are the gotchas, if any?

解决方案

您不会有一个直接引用该文件的URL。相反,您将拥有一个指向您的servlet代码的URL(或者您的服务器端框架中的某种操作代码)。反过来,在设置标题后,它必须访问文件内容并将其铲出到客户端。 (您还需要记住在适当时处理缓存控制标头。)

You wouldn't have a URL that was a direct reference to the file. Instead, you'd have a URL that leads to your servlet code (or to some sort of action code in your server-side framework). That, in turn, would have to access the file contents and shovel them out to the client, after setting up the header. (You'd also want to remember to deal with cache control headers, as appropriate.)

HttpServletResponse类具有API,可让您设置所需的所有标头。您必须确保在开始转储文件内容之前设置标题,因为标题必须首先出现在发送到浏览器的流中。

The HttpServletResponse class has APIs that'll let you set all the headers you want. You have to make sure that you set up the headers before you start dumping out the file contents, because the headers literally have to come first in the stream being sent out to the browser.

这与你可能有一个生成即时下载的servlet的情况没有多大区别。

This is not that much different from a situation where you might have a servlet that would generate a download on-the-fly.

编辑为了后人的缘故,我会把这些东西留在这里,但我会注意到有(或可能)某种方式将某些HTTP标头移交给当您存储文件时,S3会在提供文件时将Amazon退出。我不确定你是怎么做到的,而且我不确定Content-disposition是一个你可以这样设置的标题,但我会继续寻找。

edit I'll leave that stuff above here for posterity's sake, but I'll note that there is (or might be) some way to hand over some HTTP headers to S3 when you store a file, such that Amazon will spit those back out when the file is served out. I'm not exactly sure how you'd do that, and I'm not sure that "Content-disposition" is a header that you can set up that way, but I'll keep looking.

这篇关于Java webapp:添加内容处置标头以强制浏览器“另存为”行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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