java验证文件类型(未解决)待

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 < 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<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 < 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]
 <?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;
 ?>
 [/code]

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值