response.setContentType(QUOT;应用/八位字节流&QUOT) [英] response.setContentType("APPLICATION/OCTET-STREAM")

查看:130
本文介绍了response.setContentType(QUOT;应用/八位字节流&QUOT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题,可能有点傻。

I have a very basic question and it could be a little silly.

我正在编写代码来使用JSP& amp;来下载CSV文件。 Servlet的。我从互联网论坛获得了代码并且工作正常,但我想了解两行的重要性

I am writing code to download a CSV file using JSP & Servlets. I got the code from internet forums and its working fine but I am trying to understand the significance of two lines

response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\""
            + filename + "\"");

现在第一行 response.setContentType - 根据我的理解,它告诉浏览器服务器期望什么样的数据。我的问题是它告诉浏览器它的值是什么 application / octet-stream 。我看到这用于下载各种文件。如果此行通知浏览器该页面将下载文件,则导致下一行的差异有多大。 response.setHeader 将附件作为参数。

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.

any1可以告诉我这两行文件的重要性下载 ?如果我无法清楚地解释我的问题,请告诉我。

Can any1 tell me the significance of these 2 lines for file download ? Let me know if I have not been able to explain my question clearly.

关注Tarun

推荐答案

第一行描述数据类型。

response.setContentType("APPLICATION/OCTET-STREAM");

APPLICATION / OCTET-STREAM 代表二进制数据。通过指定实际文件类型可能更精确。对于图像,它可以是 image / png 。如果浏览器知道确切的文件类型,它可以直接显示文件。

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.

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.

update
在维基百科上有更好的mime类型和内容处理说明:

update There is a better explaination of mime-types and content-disposition at wikipedia:

http://en.wikipedia .org / wiki / MIME#Content-Disposition

这篇关于response.setContentType(QUOT;应用/八位字节流&QUOT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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