简体   繁体   English

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

[英]Commons-Net FTP client won't give list of files

I have this small ftp java code using which I am trying to access the files in my ubuntu machine in vmware's directory. 我有这个小的ftp java代码,我正试图使用​​它访问vmware目录中ubuntu计算机中的文件。 But I keep getting this error: 但我不断收到此错误:

    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

Code: 码:

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();
            }
        }
    }

}

I am able to download a file but I am not able to list all the files present in the directory. 我可以下载文件,但不能列出目录中存在的所有文件。 I am working on windows 8.1 home edition and running a vmware virtual machine with ubuntu operating system. 我正在使用Windows 8.1家庭版,并使用ubuntu操作系统运行vmware虚拟机。

The stack trace shows that you're missing a dependency on the ancient and totally obsolete Jakarta ORO library. 堆栈跟踪显示您缺少对古老而完全过时的Jakarta ORO库的依赖。

Commons-Net 1.4 (which you mentioned you're using) is also ancient, which is why it depends on ORO. Commons-Net 1.4(您曾说过使用的)也很古老,这就是为什么它依赖ORO的原因。 The current version is 3.3, which is what you should be using for new stuff. 当前版本是3.3,这是您应使用的新版本。 There are plenty of up-to-date examples (including for FTP) on the commons-net website ( https://commons.apache.org/proper/commons-net/ ) commons-net网站( https://commons.apache.org/proper/commons-net/ )上有很多最新的示例(包括FTP)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM