在 Java 中通过 FTP 创建文件夹层次结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4078642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a folder hierarchy through FTP in Java
提问by Pieter
Is there readily available functionality for Java to create a folder hierarchy on a remote FTP server. Apache Commons does provide an FTP client, but I can't find a method for creating a directory hierarchy. It does allow for creating a single directory (makeDirectory), but creating an entire path does not seem to be in there. The reason I want this is because sometimes part of a directory hierarchy is not (yet) available and in such a case I want to create the missing part of the hierarchy and then change to that newly created directory.
Java 是否有现成的功能可以在远程 FTP 服务器上创建文件夹层次结构。Apache Commons 确实提供了 FTP 客户端,但我找不到创建目录层次结构的方法。它确实允许创建单个目录 (makeDirectory),但似乎没有创建完整的路径。我想要这个的原因是因为有时目录层次结构的一部分(尚未)可用,在这种情况下,我想创建层次结构的缺失部分,然后更改为新创建的目录。
采纳答案by Tendayi Mawushe
You have to use a combination of FTPClient.changeWorkingDirectory
to figure out if the directory exists, then FTPClient.makeDirectory
if the call to FTPClient.changeWorkingDirectory
returns false
.
您必须使用 的组合FTPClient.changeWorkingDirectory
来确定目录是否存在,然后FTPClient.makeDirectory
如果调用FTPClient.changeWorkingDirectory
返回false
.
You need to recursively walk the directory tree in the manner described above at each level create the directory as required.
您需要以上述方式递归遍历目录树,在每个级别根据需要创建目录。
回答by Jim Garrison
Why can't you use the FTPClient#makeDirectory() method to build the hierarchy, one folder at a time?
为什么不能使用 FTPClient#makeDirectory() 方法来构建层次结构,一次一个文件夹?
回答by jjmontes
Apache Commons VFS (Virtual File System) can access several different filesystems (FTP among them), and it also provides a createFolder method that is able to create parent directories if needed:
Apache Commons VFS(虚拟文件系统)可以访问几个不同的文件系统(其中包括 FTP),它还提供了一个 createFolder 方法,可以在需要时创建父目录:
http://commons.apache.org/vfs/apidocs/org/apache/commons/vfs/FileObject.html#createFolder%28%29
http://commons.apache.org/vfs/apidocs/org/apache/commons/vfs/FileObject.html#createFolder%28%29
Documentation states that method "creates this folder, if it does not exist. Also creates any ancestor folders which do not exist. This method does nothing if the folder already exists."
文档指出该方法“创建此文件夹,如果它不存在。还会创建任何不存在的祖先文件夹。如果文件夹已经存在,则此方法不执行任何操作。”
This may suit your needs.
这可能适合您的需求。
回答by aaron.spear
Needed the answer to this and so I implemented and tested some code to create directories as needed. Hope this helps someone. cheers! Aaron
需要这个问题的答案,所以我实现并测试了一些代码来根据需要创建目录。希望这可以帮助某人。干杯!亚伦
/**
* utility to create an arbitrary directory hierarchy on the remote ftp server
* @param client
* @param dirTree the directory tree only delimited with / chars. No file name!
* @throws Exception
*/
private static void ftpCreateDirectoryTree( FTPClient client, String dirTree ) throws IOException {
boolean dirExists = true;
//tokenize the string and attempt to change into each directory level. If you cannot, then start creating.
String[] directories = dirTree.split("/");
for (String dir : directories ) {
if (!dir.isEmpty() ) {
if (dirExists) {
dirExists = client.changeWorkingDirectory(dir);
}
if (!dirExists) {
if (!client.makeDirectory(dir)) {
throw new IOException("Unable to create remote directory '" + dir + "'. error='" + client.getReplyString()+"'");
}
if (!client.changeWorkingDirectory(dir)) {
throw new IOException("Unable to change into newly created remote directory '" + dir + "'. error='" + client.getReplyString()+"'");
}
}
}
}
}
回答by aaron.spear
Use ftpSession.mkdir function to create directory.
使用 ftpSession.mkdir 函数创建目录。
@ManagedOperation
private void ftpMakeDirectory(FtpSession ftpSession, String fullDirFilePath) throws IOException {
if (!ftpSession.exists(fullDirFilePath)) {
String[] allPathDirectories = fullDirFilePath.split("/");
StringBuilder partialDirPath = new StringBuilder("");
for (String eachDir : allPathDirectories) {
partialDirPath.append("/").append(eachDir);
ftpSession.mkdir(partialDirPath.toString());
}
}
}