[英]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.