Java response.setContentType("APPLICATION/OCTET-STREAM")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23252458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
response.setContentType("APPLICATION/OCTET-STREAM")
提问by Tarun Bhatt
I have a very basic question:
我有一个非常基本的问题:
I am writing code to download a CSV file using JSP & Servlets. I got the code from internet forums and it is working fine, but I am trying to understand the significance of the two following lines
我正在编写代码以使用 JSP 和 Servlet 下载 CSV 文件。我从互联网论坛上得到了代码,它运行良好,但我试图理解以下两行的重要性
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
Now the first line response.setContentType
- According to my understanding it tells the browser what kind of data is expected from the server. My question is what does it tell the browser with value application/octet-stream
. I see this being used for downloading all kinds of files. If this lines informs the browser that page would download a file, what difference is the next line causing. response.setHeader
is having attachment as a parameter.
现在第一行response.setContentType
- 根据我的理解,它告诉浏览器期望来自服务器的数据类型。我的问题是它用 value 告诉浏览器什么application/octet-stream
。我看到这被用于下载各种文件。如果此行通知浏览器该页面将下载文件,那么下一行导致. response.setHeader
将附件作为参数。
Can anyone tell me the significance of these 2 lines for file download?
谁能告诉我这两行文件下载的重要性?
Regards Tarun
问候塔伦
采纳答案by Christian Kuetbach
The first line describes the datatype.
第一行描述数据类型。
response.setContentType("APPLICATION/OCTET-STREAM");
APPLICATION/OCTET-STREAM
stands for binary data. It may be more precise by specifing the actual filetype. For images it coud be image/png
. If the browser knows the exact filetype it may show the file directly.
APPLICATION/OCTET-STREAM
代表二进制数据。通过指定实际的文件类型可能会更精确。对于图像,它可以是image/png
。如果浏览器知道确切的文件类型,它可能会直接显示文件。
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
This says "Don't even try to show the file, just save the file." It also suggests a filename.
这表示“甚至不要尝试显示文件,只需保存文件。” 它还建议一个文件名。
updateThere is a better explaination of mime-types and content-disposition at wikipedia:
更新维基百科对 mime-types 和 content-disposition 有更好的解释: