最近碰到个烦人的事情,文件上传时没有后缀,但是下载时需要给出正确的后缀,当然主要是office的文档,索性就整个枚举类来根据contenttype找对应的后缀名了。。。。。
/**
public enum ContentTypeEnum {
DOC("application/msword", ".doc"),
DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".docx"),
RTF("application/rtf", ".rtf"),
XLS1("application/vnd.ms-excel", ".xls"),
XLS2("application/x-excel", ".xls"),
XLSX("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx"),
PPT("application/vnd.ms-powerpoint", ".ppt"),
PPTX("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx"),
PPS("application/vnd.ms-powerpoint", ".pps"),
PPSX("application/vnd.openxmlformats-officedocument.presentationml.slideshow", ".ppsx"),
PDF("application/pdf", ".pdf"),
TAR("application/x-tar", ".tar"),
TGZ("application/x-compressed", ".tgz"),
ZIP("application/x-zip-compressed", ".zip"),
Z("application/x-compress", ".z"),
BMP("image/bmp", ".bmp"),
GIF("image/gif", "gif"),
PNG("image/png", ".png"),
TIFF("image/tiff", ".tiff"),
JPEG("image/jpeg", ".jpeg"),
TXT("text/plain", ".txt"),
XML("text/xml", ".xml"),
HTML("text/html", ".html");
// 成员变量
private String contentType;
private String suffix;
// 构造方法
private ContentTypeEnum(String contentType, String suffix) {
this.contentType = contentType;
this.suffix = suffix;
}
// 普通方法
public static String getSuffixName(String contentType) {
for (ContentTypeEnum c : ContentTypeEnum.values()) {
if (c.getContentType().equals(contentType)) {
return c.suffix;
}
}
return null;
}
// 普通方法
public static String getContentTypeName(String suffix) {
for (ContentTypeEnum c : ContentTypeEnum.values()) {
if (c.getSuffix().equals(suffix)) {
return c.contentType;
}
}
return null;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}