Java 读取图片的mimeType的方法
Java 读取图片的mimeType的方法 Java 读取图片的mimeType的方法是指通过编解码的方式判断图片是否合法,从而获取图片的mimeType。这种方法可以避免通过文件后缀名来判断图片格式的不可靠性。下面是 Java 读取图片的mimeType的方法的详细介绍: 一、问题描述 在项目开发的时候,我们经常会遇到一类文件上传的问题,就是获取图片是哪种格式。很多情况下,很多人都是用后缀名去判断,如下所示: if(filename.endsWith(".png") || filename.endsWith(".jpg")){ //保存图片 }else{ throw new IOException("Error file format !"); } 但是这种方式相当不可靠,我们可以尝试将zip文件、rmvb文件、css、js修改后缀名位jpg或者png上传,也可以上传到服务器,这就造成我们服务器上出现了脏数据。此外,对于有些图片文件,修改成错误的扩展名,有些浏览器可能无法显示出此图片。 二、解决方案 在计算机系统中,媒体类型的文件都有标识符,zip、图片本身属于媒体文件,因此我们可以通过编解码的方式判断图片是否合法。下面是 Java 读取图片的mimeType的方法的核心代码: 1. 判断标示方法 private static boolean isBMP(byte[] buf){ byte[] markBuf = "BM".getBytes(); return compare(buf, markBuf); } private static boolean isICON(byte[] buf) { byte[] markBuf = {0, 0, 1, 0, 1, 0, 32, 32}; return compare(buf, markBuf); } private static boolean isWEBP(byte[] buf) { byte[] markBuf = "RIFF".getBytes(); return compare(buf, markBuf); } private static boolean isGIF(byte[] buf) { byte[] markBuf = "GIF89a".getBytes(); if(compare(buf, markBuf)) { return true; } markBuf = "GIF87a".getBytes(); if(compare(buf, markBuf)) { return true; } return false; } private static boolean isPNG(byte[] buf) { byte[] markBuf = {(byte) 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A}; return compare(buf, markBuf); } private static boolean isJPEGHeader(byte[] buf) { byte[] markBuf = {(byte) 0xff, (byte) 0xd8}; return compare(buf, markBuf); } private static boolean isJPEGFooter(byte[] buf){//JPEG结束符 byte[] markBuf = {(byte) 0xff, (byte) 0xd9}; return compare(buf, markBuf); } 2. 核心方法 private static String getMimeType(String filename){ try { String mimeType = readType(filename); return String.format("image/%s", mimeType); } catch (IOException e) { e.printStackTrace(); } return null; } private static String readType(String filename) throws IOException { FileInputStream fis = null; ... return mimeType; } 这种方法可以通过读取文件的头部信息来判断图片的格式,从而获取图片的mimeType。这种方法可以避免通过文件后缀名来判断图片格式的不可靠性,提高了图片上传的安全性和可靠性。
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![chm](https://img-home.csdnimg.cn/images/20210720083646.png)
![chm](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![chm](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 2
- 资源: 965
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)