public static String getFileType(File file){
        String res = null;
        try{
            @SuppressWarnings("resource")
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[10];
            fis.read(b, 0, b.length);
            String fileCode = bytesToHexString(b);

            Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
            while(keyIter.hasNext()){
                String key = keyIter.next();
                if(key.toLowerCase().startsWith(fileCode.toLowerCase()) || fileCode.toLowerCase().startsWith(key.toLowerCase())){
                    res = FILE_TYPE_MAP.get(key);
                    break;
                }
            }
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return res;
    }
public static String getFileType ( File file ) { String res = null ; try { @ SuppressWarnings ( "resource" ) FileInputStream fis = new FileInputStream ( file ) ; byte [ ] b = new byte [ 10 ] ; fis . read ( b , 0 , b . length ) ; String fileCode = bytesToHexString ( b ) ; Iterator < String > keyIter = FILE_TYPE_MAP . keySet ( ) . iterator ( ) ; while ( keyIter . hasNext ( ) ) { String key = keyIter . next ( ) ; if ( key . toLowerCase ( ) . startsWith ( fileCode . toLowerCase ( ) ) || fileCode . toLowerCase ( ) . startsWith ( key . toLowerCase ( ) ) ) { res = FILE_TYPE_MAP . get ( key ) ; break ; } } } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } return res ; }
获取文件类型
@param file
@return