国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java 讀取圖片的mimeType的方法

Java 讀取圖片的mimeType的方法

2021-03-25 11:15IamOkay Java教程

本篇文章主要介紹了Java 讀取圖片的mimeType的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一、問題描述

在項(xiàng)目開發(fā)的時(shí)候,我們經(jīng)常會(huì)遇到一類文件上傳的問題,就是獲取圖片是哪種格式。很多情況下,很多人都是用后綴名去判斷,如下所示。

?
1
2
3
4
5
6
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ù)。此外,對(duì)于有些圖片文件,修改成錯(cuò)誤的擴(kuò)展名,有些瀏覽器可能無法顯示出此圖片。

二、解決方案

在計(jì)算機(jī)系統(tǒng)中,媒體類型的文件都有【標(biāo)識(shí)符】,zip、圖片本身屬于媒體文件,因此我們可以通過編解碼的方式判斷圖片是否合法。

1、判斷標(biāo)示方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private static boolean isBMP(byte[] buf){
 byte[] markBuf = "BM".getBytes(); //BMP圖片文件的前兩個(gè)字節(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圖片識(shí)別符
 return compare(buf, markBuf);
 }
 
 private static boolean isGIF(byte[] buf) {
 byte[] markBuf = "GIF89a".getBytes(); //GIF識(shí)別符
 if(compare(buf, markBuf))
 {
  return true;
 }
 markBuf = "GIF87a".getBytes(); //GIF識(shí)別符
 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識(shí)別符
  // 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、核心方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
 * 獲取文件的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; //第一次讀取時(shí)已經(jīng)讀了8個(gè)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 待檢測(cè)標(biāo)示
 * @param markBuf 標(biāo)識(shí)符字節(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 輸入流對(duì)象
 * @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、測(cè)試代碼

正常測(cè)試

?
1
2
3
4
5
6
7
public class ImageType {
 public static void main(String[] args) {
  String filename = "oschina.jpg";
  String type = getMimeType(filename);
  System.out.println(type);
 }
}

輸出

image/jpeg

修改擴(kuò)展名測(cè)試

①修改oschina.jpeg為oschina.png

②復(fù)制oschina.png刪除擴(kuò)展名

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://my.oschina.net/ososchina/blog/1610685

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 老妇60一区二区三区 | 日韩福利视频 | 97碰碰碰免费公开在线视频 | 羞羞视频免费 | 久久av网 | 久久久国产一区 | 美日韩在线 | 国产精品一级 | 超碰在线9 | 亚洲一二三| 影音在线资源 | 日韩精品一区二区三区四区五区 | 亚洲精品一区久久久久久 | 成人黄色片网站 | www.久久精品 | 国产精品色婷婷亚洲综合看 | 国产区精品 | 手机av在线 | 欧美精品一区二区三区在线 | 亚洲精品片 | 亚洲免费观看 | 久久免费精品视频 | 国产高潮国产高潮久久久91 | 亚洲精品乱码 | 欧美黑人一级爽快片淫片高清 | 中文字幕第一区 | 日本好好热视频 | 久久久精品网 | 精品一区二区av | 天天操天天操 | 日韩精品极品视频在线观看免费 | 可以在线观看的av网站 | 中文字幕一二三 | 日本电影中文字幕 | 亚洲精品久久久久久动漫 | 国产精品日韩一区二区 | 综合五月 | 成人一区二区在线 | www.一区二区| 中文字幕第二页 | 欧美日韩在线一区 |