文件类型和后缀的对应关系

原创
2017/05/09 13:03
阅读数 620

最近碰到个烦人的事情,文件上传时没有后缀,但是下载时需要给出正确的后缀,当然主要是office的文档,索性就整个枚举类来根据contenttype找对应的后缀名了。。。。。

/**

  • 文件类型和后缀的枚举类<br>
  • 〈功能详细描述〉
  • @author yuanxiaobin
  • @see
  • @since */

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;
}

}

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部