2017-05-26 91 views
0

我得到505「服務器返回的HTTP響應代碼:505網址」爲下面的代碼:服務器返回的HTTP響應代碼:網址

   URL url = new URL(fileLocations.get(i)); 
       URLConnection conn = url.openConnection(); 
       InputStream in = conn.getInputStream(); 

而在瀏覽器中打開時,URL正常工作。也嘗試編碼的網址,也沒有工作。

網址是:http://52.66.123.140:8080/TATADHPFILES/1239/TDH項目/ 149387773752120170504_113201.jpg

可能是什麼原因?

+0

505是:不支持HTTP版本。 – Jens

+0

我該如何解決這個問題? –

+0

也許這將幫助你:https://dzone.com/articles/solr-tomcat-and-http11-505 – ema

回答

1

505錯誤是「HTTP錯誤505 HTTP版本不支持「(可能與」java.net。URISyntaxException:格式不正確的IPv6地址「有關)。

我通過編碼(網址),並且包裝在一個URI解決您的問題:

public static void main(String args[]) throws IOException, URISyntaxException { 
    URI uri = new URI(
      "http", 
      "52.66.123.140:8080", 
      "/TATADHPFILES/1239/TDH Items/149387773752120170504_113201.jpg", 
      "Implementation", "Java"); 
    URL url = uri.toURL(); 

    try { 
     BufferedImage img = ImageIO.read(url); 

     // --- your original code will also now work --- 
     URLConnection conn = url.openConnection(); 
     InputStream in = conn.getInputStream(); 
     // --------------------------------------- 

     System.out.println("tester"); 
    } catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
} 

我能在設定的System.out.println(使用的IntelliJ)斷點(「測試」) ; - 並能夠查看img變量(顯示「正確」的圖像)。

你原來的代碼也將正常工作。

0

我認爲這個問題的根源是,你編碼整個字符串,而不是僅編碼PARAMS:

String urlString = "someurl?param1=" + URLEncoder.encode(first, "UTF-8") + "&param2=" + URLEncoder.encode(second, "UTF-8") ; 
+0

之間「TDH項目」的空間在哪裏YPU看到URL編碼在運算的例子 – Jens

+0

因此,也許你有你的URL一些空格,請使用TRIM() – ema

0

編碼您的網址與URLEncoder的

URL url = new URL(fileLocations.get(i)); 
String encodedURL=java.net.URLEncoder.encode(url,"UTF-8"); 
System.out.println(encodedURL); 
相關問題