0

我正在尝试使用以下代码使用简单的 java 类下载文件:

简单的java类下载文件

它给我的网址以下错误:

java.net.ConnectException: Connection refused: connect  
at java.net.PlainSocketImpl.socketConnect(Native Method)  
at java.net.PlainSocketImpl.doConnect(Unknown Source)  
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)  
at java.net.PlainSocketImpl.connect(Unknown Source)  
at java.net.Socket.connect(Unknown Source)  
at java.net.Socket.connect(Unknown Source)  
at sun.net.NetworkClient.doConnect(Unknown Source)  
at sun.net.www.http.HttpClient.openServer(Unknown Source)  
at sun.net.www.http.HttpClient.openServer(Unknown Source)  
at sun.net.www.http.HttpClient.<init>(Unknown Source)  
at sun.net.www.http.HttpClient.New(Unknown Source)  
at sun.net.www.http.HttpClient.New(Unknown Source)  
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)  
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)  
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)  
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)  
at Download.URLSetUp(Download.java:32)  
at Download.main(Download.java:16)  

如果你们有任何灵魂,请告诉我。

谢谢斯内哈
_

4

3 回答 3

1

从 Java 1.5 开始,您还可以将 java.net.Proxy 实例传递给 openConnection() 方法:

//Proxy instance, proxy ip = 192.168.0.1 with port 8080  

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.1", 8080));
URL url = new URL("http://www.sheldonbrown.com/web_sample1.html");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect ();

OutputStreamWriter wr = new OutputStreamWriter(uc
              .getOutputStream());  

BufferedReader rd = new BufferedReader(new InputStreamReader(uc
                .getInputStream()));  
于 2012-04-11T11:34:27.223 回答
0

您没有与该 URL 的连接。

您是否从网络浏览器尝试过该 URL?

也许你和那个 URL 之间有代理?

于 2012-04-11T11:23:02.970 回答
0

如果您的浏览器需要代理来访问此 URL,那么您的应用程序也将需要代理。

要设置代理,请URLConnection查看此页面

这可能很简单,因为使用以下参数运行您的应用程序

java -Dhttp.proxyHost=myproxyserver.com -Dhttp.proxyPort=80 Download

于 2012-04-11T11:33:16.837 回答