java获取url后缀,以及判断是否带参数(?params=xxx)
1 // 使用了String对象的方法截取字符串 2 String fileUrl = "http://www.baidu.com?a=1234"; 3 int index = fileUrl.lastIndexOf("?"); 4 if (index != -1) { 5 fileUrl = fileUrl.substring(0, index); 6 } 7 String extension = FilenameUtils.getExtension(fileUrl); 8 System.out.println(extension);
1 # getExtension 方法 2 public static String getExtension(String filename) { 3 if (filename == null) { 4 return null; 5 } else { 6 int index = indexOfExtension(filename); 7 return index == -1 ? "" : filename.substring(index + 1); 8 } 9 }
