The URL class can be used to parse a URL and retrieve the components such as host name
String urlstring = "http://www.objects.com.au/services/sherpa.html"
URL url = new URL(urlstring};
String host = url.getHost(); // returns "www.objects.com.au"
Many developers use the toURL() method of the File class to convert a File to a URL. This method however does not handle correctly escaping characters that are illegal in URL’s (such as spaces) and has been deprecated.
To convert a File to a URL you should instead first use the toURI() method to convert the file path to a URI. The URI class has a toURL() method that can then be used to convert the URI to a URL.
File file = new File(path);
URI uri = file.toURI();
URL url = uri.toURL();
// or alternatively in one line
URL url = file.toURI().toURL();
You can use URL class to access an ftp server by using the ftp protocol with the URL
URL url = new URL("ftp://username:email@example.com/file.txt");
The URL can then be used to open a connection to the URL and read the contents
If you need more functionality than simply pulling files from the ftp server then some other options include:
- Sun’s undocumented class sun.net.ftp.FtpClient
- Jakarta Commons Net library