我正在使用FTPClient在FTP服务器中创建子目录。我想在“Root”下创建一个目录“Archive”。我使用以下函数创建子目录。
boolean s = ftNew.makeDirectory("/"+folderName+"/Archive");
但它返回false并且无法创建子目录“Archive”。怎么解决这个问题?
答案 0 :(得分:2)
FTPClient的makeDirectory返回true或false,它不是非常有用且结果很模糊。幸运的是,您可以改进代码以报告精确的FTP状态消息。
以下是您的需求:
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
}
在每个FTPClient方法之后调用它,例如:
package apachenet.ftp;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
public class App {
public static void main( String[] args ) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("127.0.0.1");
showServerReply(client);
client.login("pwyrwinski", "secret");
showServerReply(client);
System.out.println("Current working directory is: " + client.printWorkingDirectory());
String someDirectory = "nonexistentDir";
client.makeDirectory("/" + someDirectory + "/Archive");
showServerReply(client);
client.logout();
showServerReply(client);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void showServerReply(FTPClient ftpClient) {
// ...
}
}
我机器上的结果:
SERVER: 220 (vsFTPd 2.3.5)
SERVER: 230 Login successful.
Current working directory is: "/home/pwyrwinski"
SERVER: 550 Create directory operation failed.
SERVER: 221 Goodbye.
当我将String someDirectory
更改为“home / pwyrwinski”时:
SERVER: 220 (vsFTPd 2.3.5)
SERVER: 230 Login successful.
Current working directory is: "/home/pwyrwinski"
SERVER: 257 "/home/pwyrwinski/Archive" created
SERVER: 221 Goodbye.
550
是允许或拒绝访问的代码,此代码和其他代码很容易谷歌
我希望这能帮到您。
答案 1 :(得分:0)
您应该逐步创建目录。否则,您将获得创建目录false。有一个示例供您使用。 http://www.codejava.net/java-se/networking/ftp/creating-nested-directory-structure-on-a-ftp-server