2013-03-05 78 views
1

我有一個像mypage.com/a/b/somePage.html這樣的頁面,其中一個anchor href屬性就像是「a/b/anotherPage.html 」。如何從java中的相對url獲取絕對url

當我嘗試通過創建一個具有頁面url和href值的新url對象來獲取絕對url時,我獲取絕對URL爲mypage.com/a/b/a/b/anotherPage.html。

這對我造成問題,但一些瀏覽器如何正確處理這個問題。

任何可用來解決此問題的盒子。 (我總是可以在代碼中添加例外,以不同的方式處理這個問題,但我不希望這樣做。)

+0

嘗試像'網址URL =新網址(新網址(「http://www.google.com/a/b/page.html」),「a/b/page2.html」); \t System.out.println(url); ' – Srikanth 2013-03-06 16:37:56

+0

[從Java servlet中的相對URL構建絕對URL]的可能重複(http://stackoverflow.com/questions/1389184/building-an-absolute-url-from-a-relative-url-in-java -servlet) – Stephan 2013-06-02 03:16:30

回答

3

使用的java.net.URL

URL baseUrl = new URL("http:www.google.com/someFolder/"); 
URL url = new URL(baseURL , "../test.html");