如何从URL创建commons-net FTPClient

时间:2011-02-09 11:11:17

标签: java ftp

有没有办法从ftp://user:pass@foo.bar:2121/path这样的网址构建FTPClient实例,类似于JDK中的FtpURLConnection

2 个答案:

答案 0 :(得分:1)

如果您的问题是解析,那么使用下面的代码进行解析,然后创建一个包装类......

  

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

     

公共课ParseURL {
      public static void main(String [] args)抛出异常{
          URL aURL =新网址(“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());
      }}

这是程序显示的输出:
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

答案 1 :(得分:0)

没有可用的直接构造函数, 你可以用一些东西

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