道非道 非常道

          勤思、謹(jǐn)言、慎行、厚積、薄發(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 星期五 閱讀(984) 評論(0)  編輯  收藏 所屬分類: J2SE


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 安徽省| 曲阜市| 大港区| 呼图壁县| 乌兰浩特市| 文成县| 潮安县| 都兰县| 通江县| 泰州市| 鄂尔多斯市| 林甸县| 东兰县| 关岭| 姜堰市| 金溪县| 武隆县| 疏附县| 颍上县| 石台县| 台安县| 云梦县| 新津县| 阿克陶县| 沙雅县| 雷波县| 甘泉县| 麻栗坡县| 仙桃市| 青州市| 泽州县| 土默特左旗| 龙游县| 象山县| 莒南县| 台州市| 桐柏县| 新绛县| 安阳县| 札达县| 获嘉县|