简体   繁体   English

有没有办法从Java中的文件扩展名生成Content-Type标头?

[英]is there a way to generate a Content-Type header from a file extension in Java?

I have a Java application which sometimes has to generate a Content-Type header from a filename alone. 我有一个Java应用程序,有时必须从文件名单独生成Content-Type标头。 Is there a way to estimate the Content-Type for common extensions? 有没有办法估算常见扩展的内容类型? (eg ".pdf" maps to "application/pdf", etc) (例如“.pdf”映射到“application / pdf”等)

Yes, via the JavaBeans Activation Framework (javax.activation). 是的,通过JavaBeans Activation Framework (javax.activation)。

import java.io.File;
import javax.activation.MimetypesFileTypeMap;
...
return new MimetypesFileTypeMap().getContentType(new File("brognitz.jpg"));

You can also add your own content types if the Java built-in database is inadequate. 如果Java内置数据库不合适,您还可以添加自己的内容类型

Edit: Jason Day's answer is just as correct, as far as I can tell. 编辑:据我所知,Jason Day的回答是正确的。

你加载了常见类型的哈希表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:从文件中检查内容类型和扩展名(mime)的最佳方法是什么? - Java: Which is the best way to check a content-type and extension (mime) from a file? Java:一种将 Mime(内容)类型与 CommonsMultipartFile 中的文件扩展名匹配的方法 - Java: A way to match Mime (content) type to file extension from CommonsMultipartFile 在Spring中,有没有一种方法可以为标头返回不同的Content-Type值? - In Spring, Is there a way to return different Content-Type values for the header? 在 Java 中解析 Content-Type 标头而不验证字符集 - Parsing a Content-Type header in Java without validating the charset Jersey:根据文件扩展名或InputStream设置响应内容类型? - Jersey: Set response content-type based on file extension or InputStream? 使用Content-Type标头从eml文件中提取附件名称 - Extracting attachment name from eml file using Content-Type header 缺少进度侦听器Content-Type标头的FormData文件上传 - FormData File Upload with Progress Listener Content-Type header missing 使用 HttpClient4 上传文件时设置 header “Content-Type”的问题 - Problem with setting header “Content-Type” in uploading file with HttpClient4 是否可以使用文件的内容类型来确定Java中的文件类型? - Is it possible to use the content-type of a file to determine its filetype in Java? Spring是否更改Content-Type的标题? - Spring Changes Header for Content-Type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM