URLConnection FTP list files

后端 未结 3 1295
抹茶落季
抹茶落季 2020-12-11 04:09
URL url =  new URL(\"ftp://user:pass@ftp.example.com/thefolder/\");
URLConnection connection = url.openConnection();
...
// List files in folder...

相关标签:
3条回答
  • 2020-12-11 04:35

    You could use Apache commons FTPClient

    This would allow you to call listFiles with...

    public static void main(String[] args) throws IOException {
            FTPClient client = new FTPClient();
            client.connect("c64.rulez.org");
            client.enterLocalPassiveMode();
            client.login("anonymous", "");
            FTPFile[] files = client.listFiles("/pub");
            for (FTPFile file : files) {
                System.out.println(file.getName());
            }
    
    0 讨论(0)
  • 2020-12-11 04:45

    The Java SE URLConnection is insuitable for the job of retrieving a list of files from a FTP host. As to FTP, it basically only supports the FTP get or put commands (retrieve or upload file). It does not support the FTP ls command (list files) which you're basically looking for, let alone many others.

    You need to look for 3rd party libraries supporting the FTP ls command (and many more). A commonly used one is the Apache Commons Net FtpClient. In its javadoc is demonstrated how to issue a ls:

    FTPClient f = new FTPClient();
    f.connect(server);
    f.login(username, password);
    FTPFile[] files = f.listFiles(directory);
    
    0 讨论(0)
  • 2020-12-11 04:53

    Check out this class I found. It's does the lifting for you. Class at nsftools.com

    Example would be:

    FTPConnection ftpConnect = new FTPConnection();
    ftpConnect.connect("ftp.example.com");
    ftpConnect.login("user","pass");
    
    System.out.println(ftpConnect.listFiles());
    
    0 讨论(0)
提交回复
热议问题