Java获取文件ContentType

本文详细介绍了在上传文件过程中如何获取Content-Type,包括三种常见方法:通过本地文件路径、使用MimetypesFileTypeMap和FileNameMap。同时提供了Java代码示例,并展示了如何针对不同类型的文件获取其对应的Content-Type。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上传文件时,为了安全会做一些校验,检查HTTP Header中的Content-Type就是其中的一种,今天就来看下上传文件时的Content-Type是如何获取的:

先来看些常用的mimetype表:
text/plain(纯文本)
text/html(HTML文档)
text/javascript(js代码)
application/xhtml+xml(XHTML文档)
image/gif(GIF图像)
image/jpeg(JPEG图像)
image/png(PNG图像)
video/mpeg(MPEG动画)
application/octet-stream(二进制数据)
application/pdf(PDF文档)
application/(编程语言) 该种语言的代码
application/msword(Microsoft Word文件)
message/rfc822(RFC 822形式)
multipart/alternative(HTML邮件的HTML形式和纯文本形式,相同内容使用不同形式表示)
application/x-www-form-urlencoded(POST方法提交的表单)
multipart/form-data(POST提交时伴随文件上传的表单)

代码:
import java.io.File;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.activation.MimetypesFileTypeMap;

	/**   
	 * @ClassName:  GetContectType   
	 * @date:   2019年5月16日 
	 */
	public class GetContectType {
		/**
		 * 方式一
		 * 该方式只支持本地文件,有时候会存在获取为null的情况
		 * @param fileUrl
		 */
		public static String getContentTypeByLocal(String fileUrl) {
			String contentType = null;
			Path path = Paths.get(fileUrl);
	        try {
	            contentType = Files.probeContentType(path);
	        } catch (IOException e) {  
	            e.printStackTrace();
	        }
	        System.out.println("getContentTypeByLocal, File ContentType is : " + contentType);
	        return contentType;
		}
		
		/**
		 * 方式二
		 * 该方式支持本地文件,也支持http/https远程文件
		 * @param fileUrl
		 */
		public static String getContentType(String fileUrl) {
			String contentType = null;
	        try {
	        	contentType = new MimetypesFileTypeMap().getContentType(new File(fileUrl));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        System.out.println("getContentType, File ContentType is : " + contentType);
	        return contentType;
		}
		
		/**
		 * 方式三
		 * @param fileUrl,有时候会存在获取为null的情况
		 */
		public static String getContentTypeByType(String fileUrl) {
			String contentType = null;
	        try {
	        	FileNameMap fileNameMap = URLConnection.getFileNameMap();
	            contentType = fileNameMap.getContentTypeFor(fileUrl);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        System.out.println("getContentTypeByType, File ContentType is : " + contentType);
	        return contentType;
		}
		
		public static void main(String[] args) {
			// 文件路径
			String fileUrl = "C:\\Users\\***\\Downloads\\2.jpg";
			// 方式一
			getContentTypeByLocal(fileUrl);
	        
			// 方式二,推荐使用
			getContentType(fileUrl);
			
			// 方式三
			getContentTypeByType(fileUrl);
		}
	}

执行结果:
在这里插入图片描述
本文转载自:https://blog.youkuaiyun.com/p812438109/article/details/83587315
常用的mimetype表转载自 & 了解更多的文件上传漏洞可点击:
https://www.jianshu.com/p/5ebba0482980

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值