hertzsprung
hertzsprung

Reputation: 9893

How to create a commons-net FTPClient from a URL

Is there a way to construct an FTPClient instance from a URL such as ftp://user:[email protected]:2121/path, similar to FtpURLConnection in the JDK?

Upvotes: 0

Views: 1963

Answers (2)

Romain Hippeau
Romain Hippeau

Reputation: 24375

If your problem is the parsing then use the code below to parse and then just create a wrapper class ...

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

public class ParseURL {
public static void main(String[] args) throws Exception {
URL aURL = new URL("http://java.sun.com:80/docs/books/tutorial"
+ "/index.html?name=networking#DOWNLOADING");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " +
aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
} }

Here's the output displayed by the program:
protocol = http
authority = java.sun.com:80
host = java.sun.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240860

There is no direct constructor available for it, You can use something

FTPClient f = new FTPClient();
    f.connect(server);
    f.login(username, password);

Upvotes: 0

Related Questions