您的位置:首页 > 编程语言 > Java开发

java获取url地址后缀名

2014-07-16 12:27 357 查看
方法一:使用正则表达式

[java] view
plaincopy





final static Pattern pattern = Pattern.compile("\\S*[?]\\S*");

/**

* 获取链接的后缀名

* @return

*/

private static String parseSuffix(String url) {

Matcher matcher = pattern.matcher(url);

String[] spUrl = url.toString().split("/");

int len = spUrl.length;

String endUrl = spUrl[len - 1];

if(matcher.find()) {

String[] spEndUrl = endUrl.split("\\?");

return spEndUrl[0].split("\\.")[1];

}

return endUrl.split("\\.")[1];

}

方法二:使用输出流

[java] view
plaincopy





/**

* 获取链接的后缀名

* @return

*/

public static String parseSuffix(String strUrl) {

BufferedInputStream bis = null;

HttpURLConnection urlConnection = null;

URL url = null;

try {

url = new URL(strUrl);

urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.connect();

bis = new BufferedInputStream(urlConnection.getInputStream());

return HttpURLConnection.guessContentTypeFromStream(bis);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: