我正在尝试使用javax.activation.MimetypesFileTypeMap
来获取内容类型。
对于字符串“image.png”,它总是返回“application / octect-stream”...它不应该返回类似“image / png”的内容吗?
javax.activation.MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType("image.png");
答案 0 :(得分:9)
参见javax.activation.MimetypesFileTypeMap
的Javadocs。该方法按特定顺序查找名为.mime.types
的文件:
MIME类型文件搜索顺序:
MimetypesFileTypeMap在用户系统的各个位置查找MIME类型文件条目。当请求在MimetypesFileTypeMap中搜索MIME类型时,它按以下顺序搜索MIME类型文件:
- 以编程方式将条目添加到MimetypesFileTypeMap实例。
- 用户主目录中的文件
.mime.types
。- 文件
<java.home>/lib/mime.types
。- 名为
的文件或资源META-INF/mime.types
。- 名为
醇>META-INF/mimetypes.default
的文件或资源(通常仅在activation.jar
文件中找到)。
如果未找到任何文件,getContentType
方法将返回application/octet-stream
:
根据指定的文件名返回MIME类型。如上所述在 MIME类型文件搜索顺序下搜索MIME类型条目。如果没有找到条目,则类型&#34; application / octet-stream&#34;归还。
答案 1 :(得分:1)
要获取contentType,您可以使用
public String getFileContentType(String fileName) {
String fileType = "Undetermined";
final File file = new File(fileName);
try
{
fileType = Files.probeContentType(file.toPath());
}
catch (IOException ioException)
{
System.out.println(
"ERROR: Unable to determine file type for " + fileName
+ " due to exception " + ioException);
}
return fileType;
}
答案 2 :(得分:1)
我改进了MimetypesFileTypeMap(内容类型探测对于Web服务来说太慢了):
package com.github.jjYBdx4IL.utils.text;
import java.io.IOException;
import java.io.InputStream;
import javax.activation.MimetypesFileTypeMap;
//CHECKSTYLE:OFF
public class MimeType {
public static MimetypesFileTypeMap MAP = createMap();
/**
*
* @return a vastly improved mimetype map
*/
public static MimetypesFileTypeMap createMap() {
try (InputStream is = MimeType.class.getResourceAsStream("mimetypes.txt")) {
return new MimetypesFileTypeMap(is);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static String get(String fileName) {
return get(fileName, null);
}
public static String get(String fileName, String charset) {
String mimeType = MAP.getContentType(fileName.toLowerCase());
if (charset != null && (mimeType.startsWith("text/") || mimeType.contains("javascript"))) {
mimeType += ";charset=" + charset.toLowerCase();
}
return mimeType;
}
}
application/javascript js
application/msword doc docx docm
application/pdf pdf
application/postscript ai eps ps
application/rss+xml rss
application/rtf rtf
application/vnd.ms-excel xls xlsx xlsm XLS
application/vnd.ms-powerpoint ppt pps pot pptx pptm
application/vnd.oasis.database odb
application/vnd.oasis.opendocument.text odt
application/vnd.oasis.presentation odp
application/vnd.oasis.spreadsheet ods
application/vnd.oasis.text odt
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
application/x-awk awk
application/x-blender blend
application/x-cd-image iso
application/x-compress zip gz tar rar
application/x-deb deb
application/x-font-otf otf OTF
application/x-font-ttf ttf TTF
application/x-java-applet class
application/x-java-archive jar
application/xml xml
application/x-ms-dos-executable exe msi
application/x-perl pl
application/x-php php
application/x-rpm rpm
application/x-sharedlib o
application/x-shellscript sh
application/x-tar tar
application/x-texinfo texinfo texi
application/x-tex tex
application/x-trash autosave
application/x-troff t tr roff
application/x-vnd.oasis.opendocument.spreadsheet ods
application/zip zip
audio/ac3 ac3
audio/basic au
audio/midi midi mid
audio/mpeg mp3 mpeg3
audio/x-aifc aifc
audio/x-aiff aif aiff
audio/x-generic wav wma mp3 ogg
audio/x-mpeg mpeg mpg
audio/x-wav wav
image/gif gif GIF
image/ief ief
image/jpeg jpeg jpg jpe JPG
image/png png PNG
image/svg+xml svg svgz
image/tiff tiff tif
image/x-eps eps
image/x-generic bmp jpg jpeg png tif tiff xpm wmf emf
image/x-xwindowdump xwd
text/css css
text/csv csv
text/html html htm HTML HTM
text/plain txt text TXT TEXT
text/richtext rtx
text/rtf rtf
text/tab-separated-values tsv tab
text/x-bibtex bib
text/x-c++hdr h
text/x-csrc c
text/x-c++src cpp c++
text/x-java java
text/x-log log
text/xml xml XML osm
text/x-pascal pas
text/x-po po pot
text/x-python py
text/x-sql sql
text/x-tcl tcl
text/x-tex tex
video/mpeg mpeg mpg mpe mpv vbs mpegv
video/msvideo avi
video/quicktime qt mov moov
video/x-generic wmv mpeg mp4 ogv swf mov dvd osp
video/x-msvideo avi
我只是把所有可以通过Google快速找到的东西扔在一起,所以我不能说所有这些类型是否真的有效。