开发者

How would I get a relative path to an absolute path given an img src in Java?

I'm playing around with a webpage fetcher in java right now and I'm cur开发者_JS百科ious what the best way to do this in Java is.

I have a link: e.g.: http://www.nytimes.com/2010/07/08/technology/personaltech/08pogue.html?ref=technology

and when I crawl that page I might find img src paths like

"../public/images/header.jpg"
"../../test/logo.gif"

where it may be relative to one of the subdirs the file is in

Question is.. in Java is there a lib that would be able to turn these into absolute paths like

http://www.nytimes.com/2010/07/08/technology/public/images/header.jpg

?

thanks


The URL class should be able to do this, see: http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/net/URL.html#URL%28java.net.URL,%20java.lang.String%29

Eg:

URL pageURL = new URL("http://www.nytimes.com/2010/07/08/technology/personaltech/08pogue.html?ref=technology");
URL imageURL = new URL(pageURL, "../public/images/header.jpg");

Warning: not tested this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜