通过枚举类对if-else的改造

如何通过枚举,改造多重 if-else?

改造前的代码

	/**
	 * 根据“文件名的后缀”获取文件内容类型
	 * 
	 * @param fileName
	 *            文件名
	 * @return 返回文件类型
	 */
	public static String getContentType(String fileName) {
		String contentType = "application/octet-stream";
		if (fileName.lastIndexOf(".") < 0)
			return contentType;
		fileName = fileName.toLowerCase();
		fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
		if (fileName.equals("html") || fileName.equals("htm") || fileName.equals("shtml")) {
			contentType = "text/html";
		} else if (fileName.equals("apk")) {
			contentType = "application/vnd.android.package-archive";
		} else if (fileName.equals("sis")) {
			contentType = "application/vnd.symbian.install";
		} else if (fileName.equals("sisx")) {
			contentType = "application/vnd.symbian.install";
		} else if (fileName.equals("exe")) {
			contentType = "application/x-msdownload";
		} else if (fileName.equals("msi")) {
			contentType = "application/x-msdownload";
		} else if (fileName.equals("css")) {
			contentType = "text/css";
		} else if (fileName.equals("xml")) {
			contentType = "text/xml";
		} else if (fileName.equals("gif")) {
			contentType = "image/gif";
		} else if (fileName.equals("jpeg") || fileName.equals("jpg")) {
			contentType = "image/jpeg";
		} else if (fileName.equals("js")) {
			contentType = "application/x-javascript";
		} else if (fileName.equals("atom")) {
			contentType = "application/atom+xml";
		} else if (fileName.equals("rss")) {
			contentType = "application/rss+xml";
		} else if (fileName.equals("mml")) {
			contentType = "text/mathml";
		} else if (fileName.equals("txt")) {
			contentType = "text/plain";
		} else if (fileName.equals("jad")) {
			contentType = "text/vnd.sun.j2me.app-descriptor";
		} else if (fileName.equals("wml")) {
			contentType = "text/vnd.wap.wml";
		} else if (fileName.equals("htc")) {
			contentType = "text/x-component";
		} else if (fileName.equals("png")) {
			contentType = "image/png";
		} else if (fileName.equals("tif") || fileName.equals("tiff")) {
			contentType = "image/tiff";
		} else if (fileName.equals("wbmp")) {
			contentType = "image/vnd.wap.wbmp";
		} else if (fileName.equals("ico")) {
			contentType = "image/x-icon";
		} else if (fileName.equals("jng")) {
			contentType = "image/x-jng";
		} else if (fileName.equals("bmp")) {
			contentType = "image/x-ms-bmp";
		} else if (fileName.equals("svg")) {
			contentType = "image/svg+xml";
		} else if (fileName.equals("jar") || fileName.equals("var") || fileName.equals("ear")) {
			contentType = "application/java-archive";
		} else if (fileName.equals("doc")) {
			contentType = "application/msword";
		} else if (fileName.equals("pdf")) {
			contentType = "application/pdf";
		} else if (fileName.equals("rtf")) {
			contentType = "application/rtf";
		} else if (fileName.equals("xls")) {
			contentType = "application/vnd.ms-excel";
		} else if (fileName.equals("ppt")) {
			contentType = "application/vnd.ms-powerpoint";
		} else if (fileName.equals("rar")) {
			contentType = "application/x-rar-compressed";
		} else if (fileName.equals("swf")) {
			contentType = "application/x-shockwave-flash";
		} else if (fileName.equals("rpm")) {
			contentType = "application/x-redhat-package-manager";
		} else if (fileName.equals("der") || fileName.equals("pem") || fileName.equals("crt")) {
			contentType = "application/x-x509-ca-cert";
		} else if (fileName.equals("xhtml")) {
			contentType = "application/xhtml+xml";
		} else if (fileName.equals("zip")) {
			contentType = "application/zip";
		} else if (fileName.equals("mid") || fileName.equals("midi") || fileName.equals("kar")) {
			contentType = "audio/midi";
		} else if (fileName.equals("mp3")) {
			contentType = "audio/mpeg";
		} else if (fileName.equals("ogg")) {
			contentType = "audio/ogg";
		} else if (fileName.equals("m4a")) {
			contentType = "audio/x-m4a";
		} else if (fileName.equals("ra")) {
			contentType = "audio/x-realaudio";
		} else if (fileName.equals("mp4")) {
			contentType = "video/mp4";
		} else if (fileName.equals("mpeg") || fileName.equals("mpg")) {
			contentType = "video/mpeg";
		} else if (fileName.equals("mov")) {
			contentType = "video/quicktime";
		} else if (fileName.equals("flv")) {
			contentType = "video/x-flv";
		} else if (fileName.equals("m4v")) {
			contentType = "video/x-m4v";
		} else if (fileName.equals("mng")) {
			contentType = "video/x-mng";
		} else if (fileName.equals("asx") || fileName.equals("asf")) {
			contentType = "video/x-ms-asf";
		} else if (fileName.equals("wmv")) {
			contentType = "video/x-ms-wmv";
		} else if (fileName.equals("avi")) {
			contentType = "video/x-msvideo";
		}
		return contentType;
	}



不知道有没有恶心到你

改造后:

	/**
	 * 根据“文件名的后缀”获取文件内容类型
	 * 
	 * @param fileFileName
	 *            文件名
	 * @return 返回文件类型
	 */
	public static String getContentType(String fileName) {
		String contentType = "application/octet-stream";
		/* 无后缀 */
		int i = fileName.lastIndexOf(".");
		if (i < 0) {
			return contentType;
		}
		fileName = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
		contentType = MimeType.get(fileName);
		return contentType;
	}

瞬间有没有看上去更舒服的赶脚。

/**
 * @ClassName MimeType
 * @Description Mime-Type(Content-Type)文件扩展名
 * @author Cheng.Wei
 * @date 2017年12月14日 下午2:51:28
 * 
 */
public enum MimeType {
	HTM("text/html"),
	HTML("text/html"),
	SHTML("text/html"),
	APK("application/vnd.android.package-archive"),
	SIS("application/vnd.symbian.install"),
	SISX("application/vnd.symbian.install"),
	EXE("application/x-msdownload"),
	MSI("application/x-msdownload"),
	CSS("text/css"),
	XML("text/xml"),
	GIF("image/gif"),
	JPG("image/jpeg"),
	JPEG("image/jpeg"),
	JS("application/x-javascript"),
	ATOM("application/atom+xml"),
	RSS("application/rss+xml"),
	MML("text/mathml"),
	TXT("text/plain"),
	JAD("text/vnd.sun.j2me.app-descriptor"),
	WML("text/vnd.wap.wml"),
	HTC("text/x-component"),
	PNG("image/png"),
	TIF("image/tiff"),
	TIFF("image/tiff"),
	WBMP("image/vnd.wap.wbmp"),
	ICO("image/x-icon"),
	JNG("image/x-jng"),
	BMP("image/x-ms-bmp"),
	SVG("image/svg+xml"),
	JAR("application/java-archive"),
	VAR("application/java-archive"),
	EAR("application/java-archive"),
	DOC("application/msword"),
	PDF("application/pdf"),
	RTF("application/rtf"),
	XLS("application/vnd.ms-excel"),
	PPT("application/vnd.ms-powerpoint"),
	RAR("application/x-rar-compressed"),
	SWF("application/x-shockwave-flash"),
	RPM("application/x-redhat-package-manager"),
	DER("application/x-x509-ca-cert"),
	PEM("application/x-x509-ca-cert"),
	CRT("application/x-x509-ca-cert"),
	XHTML("application/xhtml+xml"),
	ZIP("application/zip"),
	MID("audio/midi"),
	MIDI("audio/midi"),
	KAR("audio/midi"),
	MP3("audio/mpeg"),
	OGG("audio/ogg"),
	M4A("audio/x-m4a"),
	RA("audio/x-realaudio"),
	MP4("video/mp4"),
	MPEG("video/mpeg"),
	MOV("video/quicktime"),
	FLV("video/x-flv"),
	M4V("video/x-m4v"),
	MNG("video/x-mng"),
	ASX("video/x-ms-asf"),
	ASF("video/x-ms-asf"),
	WMV("video/x-ms-wmv"),
	AVI("video/x-msvideo");
	
	public static boolean contains(String type){
		for(MimeType mimeType : MimeType.values()){
			if(mimeType.name().equals(type)){
				return true;
				}
			}
		return false;
	}  
	
	public  String mime;
	
	MimeType(String mime) {
		this.mime = mime;
	}
	
	public static String get(String suffix) {
		if(contains(suffix)) {
			return MimeType.valueOf(suffix).mime;
		}
		return "application/octet-stream";
	}
}

当然还有比方 key-value的形式,也可以实现。


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值