java 设置内容类型,octet-stream 和 text/csv 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14272095/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:42:20  来源:igfitidea点击:

Setting content-type, difference between octet-stream and text/csv

javaservletsmime-types

提问by Shalin

What is the difference on using response.setContentType("application/octet-stream");instead of response.setContentType("text/csv");for setting content type for a csv file, it seems they both produce the same result. Which method is preffered more? Just curious.

使用response.setContentType("application/octet-stream");而不是response.setContentType("text/csv");为 csv 文件设置内容类型有什么区别,似乎它们都产生相同的结果。哪种方法更受欢迎?只是好奇。

回答by GreyBeardedGeek

Some browsers, particularly IE, completely ignore the content type header, and open the response in an application according to the file extension. In those browsers, it probably doesn't matter.

一些浏览器,尤其是IE,完全忽略了内容类型头,根据文件扩展名在应用程序中打开响应。在那些浏览器中,这可能无关紧要。

However, text/csv is the preferred content type, and should work properly with all browsers. "application/octet-stream" is very generic, and does not give any hint as to what type of application should be used to open the result.

但是,text/csv 是首选的内容类型,应该适用于所有浏览器。“application/octet-stream”是非常通用的,并没有给出任何关于应该使用什么类型的应用程序来打开结果的提示。

If you were returning an MS Excel file, for instance, you would use application/vnd.ms-excel to be more specific. Since CSV is not tied to one particular application, text/csv is preferred.

例如,如果您要返回 MS Excel 文件,则可以使用 application/vnd.ms-excel 来更具体。由于 CSV 不依赖于某一特定应用程序,因此首选 text/csv。

回答by NPKR

There is no official RFCdocument for CSV files, but they are a flat text file so the best MIME type to use would be text/plain.

RFCCSV 文件没有官方文档,但它们是纯文本文件,因此最好使用的 MIME 类型是 text/plain。

There are OS specific MIME implementations for CSV files. For example Microsoft use application/vnd.ms-excel. 

For other MIME types refer this site : http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm

对于其他 MIME 类型,请参阅此站点:http: //webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm