簡體   English   中英

在JAVA中合並兩個URL

[英]Merge two URL in JAVA

我用下面的代碼合並兩個URL。

String strUrl1 = "http://www.domainname.com/path1/2012/04/25/file.php";
   String arg = "?page=2";
   URL url1;
    try {
        url1 = new URL(strUrl1);
        URL reconUrl1 = new URL(url1,arg);
        System.out.println(" url : " + reconUrl1.toString());
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    }

結果令我感到驚訝: http : //www.domainname.com/path1/2012/04/25/?page=2

我希望它是(瀏覽器能做什么): http : //www.domainname.com/path1/2012/04/25/file.php?page=2

關於構造函數URL(URL上下文,字符串規范)的javadoc解釋了它應遵守RFC。

我做錯了嗎?

謝謝

更新:

This is the only problem I encountered with the fonction.
The code already works in all others cases, like browser do
  "domain.com/folder/sub" + "/test" -> "domain.com/test"
  "domain.com/folder/sub/" + "test" -> "domain.com/folder/sub/test"
  "domain.com/folder/sub/" + "../test" -> "domain.com/folder/test"
  ...

您始終可以先合並字符串,然后再根據合並的字符串創建URL。

  StringBuffer buf = new StringBuffer();
  buf.append(strURL1);
  buf.append(arg);
  URL url1 = new URL(buf.toString());

嘗試

String k = url1+arg;
URL url1;
    try {
        url1 = new URL(k);
        //URL reconUrl1 = new URL(url1,arg);
        System.out.println(" url : " + url1.toString());
    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    }

我尚未閱讀RFC ,但是上下文 (如URL的Java Doc中提到的)大概是URL的目錄,這意味着URL的上下文

"http://www.domainname.com/path1/2012/04/25/file.php"

"http://www.domainname.com/path1/2012/04/25/"

這就是為什么

new URL(url1,arg);

產量

"http://www.domainname.com/path1/2012/04/25/?page=2"

“解決方法”顯然是使用+自己連接各個部分。

您在此處使用URL的構造函數,該構造函數將參數作為URL(URL context, String spec) 因此,您不要使用URL傳遞php頁面,而應使用字符串。 上下文需要是目錄。 正確的方法是

  String strUrl1 = "http://www.domainname.com/path1/2012/04/25";
  String arg = "/file.php?page=2";
  URL url1;
  try {
    url1 = new URL(strUrl1);
    URL reconUrl1 = new URL(url1,arg);
    System.out.println(" url : " + reconUrl1.toString());
 } catch (MalformedURLException ex) {
    ex.printStackTrace();
 }

嘗試這個

String strUrl1 = "http://www.domainname.com/path1/2012/04/25/";
       String arg = "file.php?page=2";
       URL url1;
        try {
            url1 = new URL(strUrl1);
            URL reconUrl1 = new URL(url1,arg);
            System.out.println(" url : " + reconUrl1.toString());
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }

當您閱讀Java文檔時,它會提及指定URL的上下文
域和路徑是:

"http://www.domainname.com"  +  "/path1/2012/04/25/"

其中"file.php"被認為是屬於上述上下文的文本。
這兩個參數重載的構造函數使用URL的上下文作為基礎,並添加第二個參數來創建完整的URL,這不是您所需要的。

因此,最好將String添加兩個部分,然后從它們創建URL:

   String contextURL = "http://www.domainname.com/path1/2012/04/25/";
   String textURL = "file.php?page=2";
   URL url;
    try {
        url = new URL(contextURL);
        URL reconUrl = new URL(url, textURL);
        System.out.println(" url : " + reconUrl.toString());
    } catch (MalformedURLException murle) {
        murle.printStackTrace();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM