有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java CommonNet FTP客户端不会给出文件列表

我有一个小的ftp java代码,我正试图使用它访问我的ubuntu机器中vmware目录下的文件。但我一直在犯这样的错误:

    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 home edition上工作,运行一台带有ubuntu操作系统的vmware虚拟机


共 (0) 个答案