Java爬虫的相对路径转绝对路径

时间:2022-11-27 13:57:59

网上看到的,摘录如下:

@SuppressWarnings("finally")
public static String getAbsoluteURL(String baseURI, String relativePath){
    String abURL=null;
    try {
        URI base=new URI(baseURI);//基本网页URI
        URI abs=base.resolve(relativePath);//解析于上述网页的相对URL,得到绝对URI
        URL absURL=abs.toURL();//转成URL
        System.out.println(absURL);
        abURL = absURL.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } finally{
        return abURL;
    }
}