1. 程式人生 > >JAVA正則表示式判斷圖片格式

JAVA正則表示式判斷圖片格式

package com.personal.test;  
import org.apache.oro.text.regex.MalformedPatternException;  
import org.apache.oro.text.regex.PatternMatcher;  
import org.apache.oro.text.regex.Perl5Matcher;  
import org.apache.oro.text.regex.Pattern;  
import org.apache.oro.text.regex.Perl5Compiler;  
import org.apache.oro.text.regex.PatternCompiler;  
/** 
 * 正則表示式 
 * <P>Title: regDemo.java</P> 
 * 
 * <P>Description: </P> 
 * 
 * <P>Copyright: Copyright (c) 2006</P> 
 * 
 * <P>Company: </P> 
 * @Date Jul 11, 2007 4:38:12 PM 
 * @author ge.tao 
 * @version 1.0 
 */ 
public class regDemo {  
    /** 
     * 判斷檔案是否圖片格式 
     * @param str 
     * @return boolean 
     * regDemo.java 
     * @author: ge.tao 
     */ 
    public boolean demo(String filename){  
        String str = filename.substring(0,filename.lastIndexOf("."));  
        String regStr = "[Gg][Ii][Ff]|[Jj][Pp][Gg]|[Bb][Mm][Pp]|[Jj][Pp][Ee][Gg]";  
        PatternCompiler compiler = new Perl5Compiler();  
        Pattern pattern = null;  
        try {  
            pattern = compiler.compile(regStr);  
        } catch (MalformedPatternException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }   
        PatternMatcher pm = new Perl5Matcher();    
        if(pm.matches(str,pattern)){  
            return true;  
        }else{  
            return false;  
        }  
    }  
}