道非道 非常道

          勤思、謹言、慎行、厚積、薄發(fā)

          統(tǒng)計

          web

          天圓

          經(jīng)濟 政治 軍事

          鍵康

          Java判斷圖片文件的類型

          轉(zhuǎn)自網(wǎng)絡(luò)
              判斷文件的類型,而不是判斷文件的后綴名



          import java.io.*;
          import javax.imageio.*;
          import java.util.*;
          import javax.imageio.stream.*;

          public class Test {
              
          public static void main(String[] args) {
                  File f 
          = new File("c://test//3.jpg");
                  
          if (f.exists()) {
                      System.out.println(getFormatInFile(f));
                  }
              }

              
          // Returns the format of the image in the file 'f'.
              
          // Returns null if the format is not known.
              public static String getFormatInFile(File f) {
                  
          return getFormatName(f);
              }

              
          // Returns the format name of the image in the object 'o'.
              
          // Returns null if the format is not known.
              private static String getFormatName(Object o) {
                  
          try {
                      
          // Create an image input stream on the image
                      ImageInputStream iis = ImageIO.createImageInputStream(o);

                      
          // Find all image readers that recognize the image format
                      Iterator iter = ImageIO.getImageReaders(iis);
                      
          if (!iter.hasNext()) {
                          
          // No readers found
                          return null;
                      }

                      
          // Use the first reader
                      ImageReader reader = (ImageReader) iter.next();

                      
          // Close stream
                      iis.close();

                      
          // Return the format name
                      return reader.getFormatName();
                  } 
          catch (IOException e) {
                      
          //
                  }

                  
          // The image could not be read
                  return null;
              }
          }


          posted on 2010-03-17 15:04 星期五 閱讀(978) 評論(0)  編輯  收藏 所屬分類: J2SE


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 观塘区| 夹江县| 广丰县| 鄂温| 同江市| 文化| 华亭县| 崇明县| 乌鲁木齐县| 洛川县| 陵川县| 长葛市| 介休市| 松原市| 清流县| 文成县| 武城县| 昌吉市| 贵溪市| 桓台县| 延吉市| 威海市| 漳平市| 潞城市| 珲春市| 收藏| 双流县| 双鸭山市| 辰溪县| 清涧县| 金华市| 图木舒克市| 庆阳市| 新龙县| 苏尼特右旗| 色达县| 肥乡县| 上犹县| 金川县| 金塔县| 临江市|