I'm trying to write a simple program that displays the content of various URLS. My code is this.
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
public class test {
public static void main(String[] args) {
URL url;
//String site ="ftp://ftp.suse.com/";
//String site ="http://www.google.ca";
//String site = "ftp://ftp.gnu.org/README";
String site = "ftp://metalab.unc.edu/";
try {
url = new URL(site);
InputStream stream = url.openStream();
for(int i = 0;i!= -1;i= stream.read()){
System.out.print((char)i);
}
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The first 3 resources are fine
- String site ="ftp://ftp.suse.com/";
- String site = "http://www.google.ca";
- String site = "ftp://ftp.gnu.org/README";
but the last one
- String site = "ftp://metalab.unc.edu/";
produces the following error
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(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.ftp.impl.FtpClient.openPassiveDataConnection(Unknown Source)
at sun.net.ftp.impl.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.impl.FtpClient.list(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at test.main(test.java:13)
This also happens with various other ftp sites that I have tried as well. Haven't had any problems with HTTP sites. Any ideas what I can do to fix this. All the specified resources I can reach from my browser.