Commons-Net FTP 客户端不会提供文件列表
Commons-Net FTP client won't give list of files
我有这个小 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.run(Unknown Source)
at java.net.URLClassLoader.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)
我有这个小 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.run(Unknown Source)
at java.net.URLClassLoader.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)