Java类下载文件

时间:2012-04-11 11:19:16

标签: java url connection download connectexception

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

Simple java class to download the file

它为我的网址提供了以下错误:

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)  

如果你有任何人有任何想法,请告诉我。

感谢
斯纳

3 个答案:

答案 0 :(得分: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()));  

答案 1 :(得分:0)

您与该网址没有关联。

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

也许您和该网址之间存在代理?

答案 2 :(得分:0)

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

要为URLConnection设置代理,请查看this page

使用以下参数运行应用程序可能很简单

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