這篇文章主要介紹Java如何讀取圖片的mimeType,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、雄縣網(wǎng)絡(luò)推廣、小程序定制開發(fā)、雄縣網(wǎng)絡(luò)營銷、雄縣企業(yè)策劃、雄縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供雄縣建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.chinadenli.net
一、問題描述
在項目開發(fā)的時候,我們經(jīng)常會遇到一類文件上傳的問題,就是獲取圖片是哪種格式。很多情況下,很多人都是用后綴名去判斷,如下所示。
if(filename.endsWith(".png") || filename.endsWith(".jpg"))
{
//保存圖片
}else{
throw new IOException("Error file format !");
}但是這種方式相當(dāng)不可靠,我們可以嘗試將zip文件、rmvb文件、css、js修改后綴名位jpg或者png上傳,也可以上傳到服務(wù)器,這就造成我們服務(wù)器上出現(xiàn)了臟數(shù)據(jù)。此外,對于有些圖片文件,修改成錯誤的擴(kuò)展名,有些瀏覽器可能無法顯示出此圖片。
二、解決方案
在計算機(jī)系統(tǒng)中,媒體類型的文件都有【標(biāo)識符】,zip、圖片本身屬于媒體文件,因此我們可以通過編解碼的方式判斷圖片是否合法。
1、判斷標(biāo)示方法
private static boolean isBMP(byte[] buf){
byte[] markBuf = "BM".getBytes(); //BMP圖片文件的前兩個字節(jié)
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(); //WebP圖片識別符
return compare(buf, markBuf);
}
private static boolean isGIF(byte[] buf) {
byte[] markBuf = "GIF89a".getBytes(); //GIF識別符
if(compare(buf, markBuf))
{
return true;
}
markBuf = "GIF87a".getBytes(); //GIF識別符
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}; //PNG識別符
// new String(buf).indexOf("PNG")>0 //也可以使用這種方式
return compare(buf, markBuf);
}
private static boolean isJPEGHeader(byte[] buf) {
byte[] markBuf = {(byte) 0xff, (byte) 0xd8}; //JPEG開始符
return compare(buf, markBuf);
}
private static boolean isJPEGFooter(byte[] buf)//JPEG結(jié)束符
{
byte[] markBuf = {(byte) 0xff, (byte) 0xd9};
return compare(buf, markBuf);
}2、核心方法
/**
* 獲取文件的mimeType
* @param filename
* @return
*/
private static String getMimeType(String filename){
try {
String mimeType = readType(filename);
return String.format("image/%s", mimeType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 讀取文件類型
* @param filename
* @return
* @throws IOException
*/
private static String readType(String filename) throws IOException {
FileInputStream fis = null;
try {
File f = new File(filename);
if(!f.exists() || f.isDirectory() || f.length()<8) {
throw new IOException("the file ["+f.getAbsolutePath()+"] is not image !");
}
fis= new FileInputStream(f);
byte[] bufHeaders = readInputStreamAt(fis,0,8);
if(isJPEGHeader(bufHeaders))
{
long skiplength = f.length()-2-8; //第一次讀取時已經(jīng)讀了8個byte,因此需要減掉
byte[] bufFooters = readInputStreamAt(fis, skiplength, 2);
if(isJPEGFooter(bufFooters))
{
return "jpeg";
}
}
if(isPNG(bufHeaders))
{
return "png";
}
if(isGIF(bufHeaders)){
return "gif";
}
if(isWEBP(bufHeaders))
{
return "webp";
}
if(isBMP(bufHeaders))
{
return "bmp";
}
if(isICON(bufHeaders))
{
return "ico";
}
throw new IOException("the image's format is unkown!");
} catch (FileNotFoundException e) {
throw e;
}finally{
try {
if(fis!=null) fis.close();
} catch (Exception e) {
}
}
}
/**
* 標(biāo)示一致性比較
* @param buf 待檢測標(biāo)示
* @param markBuf 標(biāo)識符字節(jié)數(shù)組
* @return 返回false標(biāo)示標(biāo)示不匹配
*/
private static boolean compare(byte[] buf, byte[] markBuf) {
for (int i = 0; i < markBuf.length; i++) {
byte b = markBuf[i];
byte a = buf[i];
if(a!=b){
return false;
}
}
return true;
}
/**
*
* @param fis 輸入流對象
* @param skiplength 跳過位置長度
* @param length 要讀取的長度
* @return 字節(jié)數(shù)組
* @throws IOException
*/
private static byte[] readInputStreamAt(FileInputStream fis, long skiplength, int length) throws IOException
{
byte[] buf = new byte[length];
fis.skip(skiplength); //
int read = fis.read(buf,0,length);
return buf;
}3、測試代碼
正常測試
public class ImageType {
public static void main(String[] args) {
String filename = "oschina.jpg";
String type = getMimeType(filename);
System.out.println(type);
}
}輸出
image/jpeg
修改擴(kuò)展名測試
①修改oschina.jpeg為oschina.png
②復(fù)制oschina.png刪除擴(kuò)展名
public class ImageType {
public static void main(String[] args) {
String filename = "oschina.png";
String type = getMimeType(filename);
System.out.println(type);
filename = "oschina";
type = getMimeType(filename);
System.out.println(type);
}
}輸出
image/jpeg
image/jpeg
Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
以上是“Java如何讀取圖片的mimeType”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:Java如何讀取圖片的mimeType
網(wǎng)頁網(wǎng)址:http://www.chinadenli.net/article36/iiocpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、App設(shè)計、全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)