java URL根据相对路径获取绝对路径

参考文章:http://jsczxy2.iteye.com/blog/1683516


public static String getAbsUrl(String absolutePath, String relativePath){
		try {
			URL absoluteUrl = new URL(absolutePath); 
			URL parseUrl = new URL(absoluteUrl ,relativePath );
			return parseUrl.toString();
		} 
		catch (MalformedURLException e) {
			return "";
		}
	}

------------------------------------------------------------------

System.out.println(getAbsUrl("http://www.aaa.com/1/2/3.html", "../../a.jpg"));
System.out.println(getAbsUrl("http://www.aaa.com/1/2/3.html", "a.jpg"));
System.out.println(getAbsUrl("http://www.aaa.com/1/2/3.html", "/a.jpg"));
System.out.println(getAbsUrl("http://www.aaa.com/1/2/3.html", "http://www.aaa.com/3.jpg"));

结果输出:

http://www.aaa.com/a.jpg
http://www.aaa.com/1/2/a.jpg
http://www.aaa.com/a.jpg
http://www.aaa.com/3.jpg

你可能感兴趣的:(Java)