使用Java通过FTP将所有目录复制到服务器 [英] Copy all directories to server by FTP using Java
本文介绍了使用Java通过FTP将所有目录复制到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将目录从本地磁盘复制到服务器。
该目录包含许多目录,子目录和文件。 (想象一下目录的层次树)。
以下是复制一个文件的示例:
public void saveFilesToServer()throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect(ftp.foobar.com);
if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
ftp.disconnect();
log.fatal(FTP未断开连接);
}
ftp.login(foo,qwerty);
log.info(连接到服务器。);
log.info(ftp.getReplyString());
ftp.changeWorkingDirectory(test);
ftp.makeDirectory(somedir);
ftp.changeWorkingDirectory(somedir);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
java.io.File srcFolder = new java.io.File(folderPath);
FileInputStream fis = new FileInputStream(srcFolder);
ftp.storeFile(fileName,fis);
ftp.disconnect();
log.info(FTP已断开连接);
$ / code>
现在,我需要复制一个目录( somedir
),所有的子目录和文件都是 somedir
。
我认为算法应该使用递归。
是否有人知道如何?
解决方案
以下是该问题的递归解决方案的示例:
public void saveFilesToServer(String remoteDest,File localSrc)throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect(ftp.foobar.com);
if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
ftp.disconnect();
log.fatal(FTP未断开连接);
}
ftp.login(foo,qwerty);
log.info(连接到服务器。);
log.info(ftp.getReplyString());
ftp.changeWorkingDirectory(remoteDest);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
尝试{
upload(localSrc,ftp);
}
finally {
ftp.disconnect();
log.info(FTP已断开连接);
$ b公共无效上传(File src,FTPClient ftp)抛出IOException {
if(src.isDirectory()){
ftp。 makeDirectory(src.getName());
ftp.changeWorkingDirectory(src.getName());
(文件file:src.listFiles()){
upload(file,ftp);
}
ftp.changeToParentDirectory();
}
else {
InputStream srcStream = null;
尝试{
srcStream = src.toURI()。toURL()。openStream();
ftp.storeFile(src.getName(),srcStream);
}
finally {
IOUtils.closeQuietly(srcStream);
}
}
}
IOUtils
是 Apache Commons IO 的一部分。
I need to copy a directory from the local disk to a server. The directory contains a lot of directories, subdirectories, and files. (Think of a hierarchy tree of directories).
Here is an example to copy one file:
public void saveFilesToServer() throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect(ftp.foobar.com);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
log.fatal("FTP not disconnected");
}
ftp.login("foo", "qwerty");
log.info("Connected to server .");
log.info(ftp.getReplyString());
ftp.changeWorkingDirectory("test");
ftp.makeDirectory("somedir");
ftp.changeWorkingDirectory("somedir");
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
java.io.File srcFolder = new java.io.File(folderPath);
FileInputStream fis = new FileInputStream(srcFolder);
ftp.storeFile (fileName, fis);
ftp.disconnect();
log.info("FTP disconnected");
}
Now, I need to copy a directory (somedir
) with all the subdirectories and files of somedir
.
I think the algorithm should use recursion. Does someone know how?
解决方案
The following is an example of a recursive solution to the problem:
public void saveFilesToServer(String remoteDest, File localSrc) throws IOException {
FTPClient ftp = new FTPClient();
ftp.connect("ftp.foobar.com");
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
log.fatal("FTP not disconnected");
}
ftp.login("foo", "qwerty");
log.info("Connected to server .");
log.info(ftp.getReplyString());
ftp.changeWorkingDirectory(remoteDest);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
try {
upload(localSrc, ftp);
}
finally {
ftp.disconnect();
log.info("FTP disconnected");
}
}
public void upload(File src, FTPClient ftp) throws IOException {
if (src.isDirectory()) {
ftp.makeDirectory(src.getName());
ftp.changeWorkingDirectory(src.getName());
for (File file : src.listFiles()) {
upload(file, ftp);
}
ftp.changeToParentDirectory();
}
else {
InputStream srcStream = null;
try {
srcStream = src.toURI().toURL().openStream();
ftp.storeFile(src.getName(), srcStream);
}
finally {
IOUtils.closeQuietly(srcStream);
}
}
}
IOUtils
is a part of Apache Commons IO.
这篇关于使用Java通过FTP将所有目录复制到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文