Java 服务器返回了URL的HTTP响应代码:505
我得到以下代码的“服务器返回HTTP响应代码:505 for URL”:Java 服务器返回了URL的HTTP响应代码:505,java,spring,Java,Spring,我得到以下代码的“服务器返回HTTP响应代码:505 for URL”: URL url = new URL(fileLocations.get(i)); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); 而URL在浏览器中打开时工作正常。还尝试对url进行编码,但也不起作用 URL为
URL url = new URL(fileLocations.get(i));
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
而URL在浏览器中打开时工作正常。还尝试对url进行编码,但也不起作用
URL为:Items/149387773752120170504_113201.jpg
原因可能是什么?我认为问题的根本原因是您对整个字符串进行编码,而不是只对参数进行编码:
String urlString = "someurl?param1=" + URLEncoder.encode(first, "UTF-8") + "¶m2=" + URLEncoder.encode(second, "UTF-8") ;
使用URLEncoder对url进行编码
URL url = new URL(fileLocations.get(i));
String encodedURL=java.net.URLEncoder.encode(url,"UTF-8");
System.out.println(encodedURL);
505错误是“HTTP错误505 HTTP版本不受支持”(这可能与“java.net.URISyntaxException:格式错误的IPv6地址”有关)
我通过编码(URL)并封装在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());
}
}
我能够(使用Intellij)在System.out.println(“测试仪”)上设置断点并且能够查看img变量(显示“正确”的图像)
您的原始代码也可以工作。505是:不支持HTTP版本。我如何修复此问题?也许这将帮助您:我将获得
HTTP状态404
url@MohammedsalimShivani问题中的URL是一个两行的URL。在op的示例中,ypu在哪里看到URL编码?因此,您的URL中可能有一些空格,请使用trim()