java - Commons-Net FTP 客户端不会提供文件列表

标签 java ftp apache-commons-net

我有一个小的 ftp java 代码,我试图使用它来访问 vmware 目录中 ubuntu 机器中的文件。但我不断收到此错误:

    Current directory is /home/username/Documents
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException
    at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createUnixFTPEntryParser(DefaultFTPFileEntryParserFactory.java:169)
    at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2358)
    at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2141)
    at edp_ftp_client.FtpClientMain.main(FtpClientMain.java:54)
Caused by: java.lang.ClassNotFoundException: org.apache.oro.text.regex.MalformedPatternException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more

代码:

public class FtpClientMain {

    public static void main(String[] args) {
        String server = "hostname.example.com";
        int port = 21;
        String user = "username";
        String pass = "password";
        String directory = "/home/username/Documents/";
        String dwn_directory = "C:/Users/username/Desktop/files/";
        String f_name = "image";
        String filename;
        String extention = ".jpg";
        String full_name, dwn_full_name;
        int rc = 0;
        int dir_found = 0, file_found = 0;
        int exit = 0;

        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            for(int i = 1; i<50 ;i++) {
                full_name = directory + f_name + i + extention;
                dwn_full_name = dwn_directory + f_name + i + extention;
                filename = f_name + i + extention;
                ftpClient.changeWorkingDirectory(directory);
                rc = ftpClient.getReplyCode();
                if(rc == 550) {
                    System.out.println("Directory not found");
                    break;
                }
                System.out.println("Current directory is " + ftpClient.printWorkingDirectory());

                //get list of filenames
                FTPFile[] ftpFiles = ftpClient.listFiles(ftpClient.printWorkingDirectory());

                if (ftpFiles != null && ftpFiles.length > 0) {
                    //loop thru files
                    for (FTPFile file : ftpFiles) {
                        if (!file.isFile()) {
                            continue;
                        }
                        System.out.println("Found a file");
                        System.out.println("File is " + file.getName());
                        //get output stream
                        OutputStream output;
                        output = new FileOutputStream("FtpFiles" + "/" + file.getName());
                        //get the file from the remote system
                        ftpClient.retrieveFile(file.getName(), output);
                        //close output stream
                        output.close();

                        //delete the file
                        // ftp.deleteFile(file.getName());

                    }
                }
                /*File download_file = new File(dwn_full_name);
                OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(download_file));
                boolean success = ftpClient.retrieveFile(full_name, outputStream);
                outputStream.close();

                if (success) {
                    System.out.println("File #1 has been downloaded successfully.");
                }*/
            }

        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

}

我可以下载文件,但无法列出目录中存在的所有文件。 我正在使用 Windows 8.1 家庭版并运行带有 ubuntu 操作系统的 vmware 虚拟机。

最佳答案

堆栈跟踪显示您缺少对古老且完全过时的 Jakarta ORO 库的依赖。

Commons-Net 1.4(您提到您正在使用)也是古老的,这就是它依赖于 ORO 的原因。当前版本是 3.3,这是您应该用于新内容的版本。 commons-net 网站 ( https://commons.apache.org/proper/commons-net/ ) 上有大量最新示例(包括 FTP)

关于java - Commons-Net FTP 客户端不会提供文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29335265/

相关文章:

java - 未找到方案 : smtp 的组件

java - BlackBerry:检索当前年、月、日

linux - 多次零星文件传输后 FTP 突然拒绝连接

java - 上传文件的FTP服务器

java - 对于哪些类型变量绝对不能是静态的?

java - JAVA中如何从目录的子文件夹中读取特定类型的文件

php - 使用 PHP 时如何获取 FTP 错误

linux - CentOS 上 Proftpd 登录失败

Java FTPClient (apache commons) storefile() 成功上传文件,然后超时..?

java - commons-net FTPClient.retrieveFileStream() 返回错误结果