2017-05-26 147 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); 
相关问题