---
title: "java验证文件类型（未解决）待"
author: "vtyi"
date: "2009-09-01T23:27:41Z"
tags: []
slug: "java"
draft: false
lastmod: "2009-09-01T23:27:41Z"
id: 460383
url: "https://vtyi.iteye.com/blog/460383"
---

import java.awt.Desktop;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.text.SimpleDateFormat;  
import java.util.Iterator;

/\**  
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
 * Copyright 2009  . All rights reserved.  
 *  
 * This software is the confidential and proprietary information  
 * of vtyi . You shall not disclose such Confidential Information  
 * and shall use it only in accordance with the terms of the license  
 * agreement you entered into with vtyi.  
 *  
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
 * Revision History  
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
 * Date                   Author       Description  
 * 2009-9-1              vtyi  
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
 \*/

/\**  
 * @author vtyi  
 *  
 \*/  
import javax.activation.\*;  
import javax.imageio.ImageIO;  
import javax.imageio.ImageReader;  
import javax.imageio.stream.ImageInputStream;  
public class FileCheck {

    /\**  
     * @param args  
     \*/  
    public static void main(String\[] args) throws Exception {  
        /*  
        bool xx=false;  //default sFileName is not Exe or Dll File  
        System.IO.FileStream  fs=new System.IO.FileStream(sFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);  
        System.IO.BinaryReader r=new System.IO.BinaryReader(fs);  
        string bx="";  
        byte buffer;  
        try  
        {  
            buffer=r.ReadByte();  
            bx=buffer.ToString();  
            buffer=r.ReadByte();  
            bx+=buffer.ToString();

        }  
        catch (Exception exc)  
        {  
            Console.WriteLine(exc.Message);  
        }  
        r.Close();  
        fs.Close();  
        if (bx=="7790"||bx=="8297"||bx=="8075")//7790:exe,8297:rar,8075:pk  
        {  
            xx=true;  
        }  
        Console.WriteLine(bx);  
        return xx;                 dll:MZ  
        exe:MZ  
        rar:Rar  
        zip:PK  
\*/  
        File file=new File("F:\\\\wrar39b3sc.exe");  
        File file1=new File("E:\\\\mail\\\\20080405(006).jpg");  
         
        String str=javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemTypeDescription(file);  
        String str2=javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemTypeDescription(file);  
        System.out.println(str);  
        System.out.println(str2);  
         
        FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();  
        String path;  
        if (args.length == 0) {  
         path = "E:/mail/";  
        } else {  
         path = args\[0];  
        }  
        File dir = new File(path);  
        File files\[] = dir.listFiles();  
        for (int i = 0; i &lt; files.length; i++) {  
         File file2 = files\[i];  
         System.out  
           .println(file2.getName() + ": " + map.getContentType(file2));  
        }  
      //  disp();  
        getimage();  
        FileInputStream fis = new FileInputStream(new File("D:/目录结构说明.xsss"));  
        String Excel="d0cf11e0a1b11ae100000000000000003e030feff9060000000000010001";  
                    //d0cf11e0a1b11ae100000000000000003e030feff9060000000000010001  
                    //d0cf11e0a1b11ae100000000000000003e030feff90600000000000100022  
        StringBuffer sb=new StringBuffer();  
        for(int i=0;i&lt;fis.available();i++) {  
            String strs=Integer.toHexString(fis.read());  
             
        sb.append(strs);  
        }  
        if(sb.toString().substring(0,Excel.length()).equals(Excel))  
            System.out.println("是EXCEL文件");  
        else System.out.println("不是EXCEL文件");

    }  
     
    public static void disp(){  
        if (!Desktop.isDesktopSupported()) {  
            System.err.println("Desktop not supported!");  
            System.exit(-1);  
           }  
           Desktop desktop = Desktop.getDesktop();  
           String path;  
           
            path = "E:/mail/";  
           
           File dir = new File(path);  
           File files\[] = dir.listFiles();  
           for (int i = 0; i &lt; files.length; i++) {  
            File file = files\[i];  
            if (desktop.isSupported(Desktop.Action.OPEN)) {  
             System.out.println("Opening... " + file.getName());  
             try {  
              desktop.open(file);  
             } catch (IOException ioe) {  
              System.err.println("Unable to open: " + file.getName());  
             }  
            }

           }

    }  
    private static String getFormatNames(Object o) {    
        try{    
           ImageInputStream iis = ImageIO.createImageInputStream(o);                
           Iterator iter = ImageIO.getImageReaders(iis);    
            if(!iter.hasNext()){    
                return null;    
            }    
            ImageReader reader = (ImageReader) iter.next();    
            iis.close();    
            return reader.getFormatName();    
        }catch(IOException e){    
            e.printStackTrace();    
        }    
        return null;     
   }   
     
    public static void getimage() throws Exception{  
        File file = new File("F:\\\\My Pictures\\\\Pictures");    
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh:mm");    
        FileTypeMap  ft = FileTypeMap.getDefaultFileTypeMap();    
        File\[] files = file.listFiles();    
        if(!file.isDirectory()){    
            System.out.println("不是目录");    
        }else{    
            for (File f : files) {  
              if(f.isFile()&&null!=getFormatNames(f)){    
              BufferedImage img = ImageIO.read(f);  
              System.out.println("类型"+getFormatNames(f));    
              }    
              System.out.println(ft.getContentType(f));    
              ft.setDefaultFileTypeMap(ft);    
                  
            }    
        }

    }  
}  
/\**  
\[code]  
 &lt;?php  
 $filename = "D:\\\\296.mid";  
 $file     = fopen($filename, "rb");  
 $bin      = fread($file, 2); //只读2字节  
 fclose($file);  
 $strInfo  = @unpack("c2chars", $bin);  
 $typeCode = intval($strInfo\['chars1'].$strInfo\['chars2']);  
 $fileType = '';  
 switch ($typeCode)  
 {  
         case 7790:  
                 $fileType = 'exe';  
                 break;  
         case 7784:  
                 $fileType = 'midi';  
                 break;  
         case 8297:  
                 $fileType = 'rar';  
                 break;  
         case 255216:  
                 $fileType = 'jpg';  
                 break;  
         case 7173:  
                 $fileType = 'gif';  
                 break;  
         case 6677:  
                 $fileType = 'bmp';  
                 break;  
         case 13780:  
                 $fileType = 'png';  
                 break;  
         default:  
                 echo 'unknown';  
 }

 echo 'this is a(an) '.$fileType.' file:'.$typeCode;  
 ?&gt;  
 \[/code]

\*/