利用正則匹配url是否合法對於有的url會浪費過長時間使程式卡死,切記!
阿新 • • 發佈:2018-11-22
改進:改成匹配url是否為以某個結尾的,至於非法的url就讓Jsoup.connect(url)把異常拋棄
//啟動該正則匹配特別的慢 // public static String regex = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$"; public static final Pattern FILTERS = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g" + "|png|tiff?|mid|mp2|mp3|mp4" + "|wav|avi|mov|mpeg|ram|m4v|pdf" + "|rm|smil|wmv|swf|wma|zip|rar|gz))$"); public static boolean isMatchURL(String url) { // Pattern pattern = Pattern.compile(regex); if(FILTERS.matcher(url).matches()) { return false; } // if (pattern.matcher(url).matches()) { // return true; // } return true; }