Java判断图片格式的代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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
import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
import javax.imageio.ImageIO; 
import javax.imageio.ImageReader; 
import javax.imageio.stream.ImageInputStream; 
   
public class ImageDemo { 
   
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
           
        File file = new File("E:/download_test/pics/1c/1cd5v0uya36wg0rf4fu39dtym.png"); 
           
        boolean result = isImage(file); 
        System.out.println("result="+result); 
           
        getExtension(file); 
    
       
    public static void getExtension(File file) { 
        ImageInputStream iis = null
        try
            iis = ImageIO.createImageInputStream(file); 
            Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);  
            if(iter.hasNext()){ 
                System.out.println("扩展名:"+iter.next().getFormatName()); 
            
        } catch (IOException e) { 
            e.printStackTrace(); 
        }finally
            if(iis!=null){ 
                try
                    iis.close(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                
            
        
    
   
    public static boolean isImage(File resFile){ 
        ImageInputStream iis = null
        try
            iis = ImageIO.createImageInputStream(resFile); 
            Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);  
            if (iter.hasNext()) {//文件不是图片  
                return true
            }  
               
        } catch (IOException e) { 
            e.printStackTrace(); 
        }finally
            if(iis!=null){ 
                try
                    iis.close(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                
            
        
        return false
    
       
}