java.net.SocketException:使用SocketInputStream重置连接

时间:2015-10-06 15:36:27

标签: java servlets stream

我尝试从URL下载文件并将其保存在本地java中。这个URL可以在我的浏览器中运行,但在JAVA中,我得到了这些例外: java.net.SocketException:连接重置。

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:658)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
    at java.net.URL.openStream(URL.java:1037)

我尝试了不同的实现:

Apache Common IO

Files.copy(myUrl.openStream(), file, StandardCopyOption.REPLACE_EXISTING);

泽西2

WebTarget target =ClientBuilder.newClient().target(myUri);
Response response = target.path(ressourcePath).request().get();

Java io

URL link = new URL(myUri);
InputStream in = new BufferedInputStream(link.openStream());

所有这些示例都抛出一个java.net.SocketException:Connection reset Exception。

我无法控制发送文件的服务器。

1 个答案:

答案 0 :(得分:0)

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {
    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
    }
}