mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
feat: hutool-extra ftp 支持上传文件或目录(包含当前及子孙目录的所有文件)
This commit is contained in:
parent
8a30943d44
commit
6d83db671b
@ -163,13 +163,13 @@ public abstract class AbstractFtp implements Closeable {
|
|||||||
if (StrUtil.isNotEmpty(s)) {
|
if (StrUtil.isNotEmpty(s)) {
|
||||||
boolean exist = true;
|
boolean exist = true;
|
||||||
try {
|
try {
|
||||||
if (false == cd(s)) {
|
if (!cd(s)) {
|
||||||
exist = false;
|
exist = false;
|
||||||
}
|
}
|
||||||
} catch (FtpException e) {
|
} catch (FtpException e) {
|
||||||
exist = false;
|
exist = false;
|
||||||
}
|
}
|
||||||
if (false == exist) {
|
if (!exist) {
|
||||||
//目录不存在时创建
|
//目录不存在时创建
|
||||||
mkdir(s);
|
mkdir(s);
|
||||||
cd(s);
|
cd(s);
|
||||||
|
@ -21,6 +21,7 @@ import java.nio.charset.Charset;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FTP客户端封装<br>
|
* FTP客户端封装<br>
|
||||||
@ -219,7 +220,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
final int replyCode = client.getReplyCode(); // 是否成功登录服务器
|
final int replyCode = client.getReplyCode(); // 是否成功登录服务器
|
||||||
if (false == FTPReply.isPositiveCompletion(replyCode)) {
|
if (!FTPReply.isPositiveCompletion(replyCode)) {
|
||||||
try {
|
try {
|
||||||
client.disconnect();
|
client.disconnect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -354,7 +355,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
String fileName;
|
String fileName;
|
||||||
for (FTPFile ftpFile : ftpFiles) {
|
for (FTPFile ftpFile : ftpFiles) {
|
||||||
fileName = ftpFile.getName();
|
fileName = ftpFile.getName();
|
||||||
if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) {
|
if (!StrUtil.equals(".", fileName) && !StrUtil.equals("..", fileName)) {
|
||||||
if (null == filter || filter.accept(ftpFile)) {
|
if (null == filter || filter.accept(ftpFile)) {
|
||||||
result.add(ftpFile);
|
result.add(ftpFile);
|
||||||
}
|
}
|
||||||
@ -375,7 +376,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
String pwd = null;
|
String pwd = null;
|
||||||
if (StrUtil.isNotBlank(path)) {
|
if (StrUtil.isNotBlank(path)) {
|
||||||
pwd = pwd();
|
pwd = pwd();
|
||||||
if (false == cd(path)) {
|
if (!cd(path)) {
|
||||||
throw new FtpException("Change dir to [{}] error, maybe path not exist!", path);
|
throw new FtpException("Change dir to [{}] error, maybe path not exist!", path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -439,7 +440,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
final String pwd = pwd();
|
final String pwd = pwd();
|
||||||
final String fileName = FileUtil.getName(path);
|
final String fileName = FileUtil.getName(path);
|
||||||
final String dir = StrUtil.removeSuffix(path, fileName);
|
final String dir = StrUtil.removeSuffix(path, fileName);
|
||||||
if (false == cd(dir)) {
|
if (!cd(dir)) {
|
||||||
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
|
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,7 +471,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
childPath = StrUtil.format("{}/{}", dirPath, name);
|
childPath = StrUtil.format("{}/{}", dirPath, name);
|
||||||
if (ftpFile.isDirectory()) {
|
if (ftpFile.isDirectory()) {
|
||||||
// 上级和本级目录除外
|
// 上级和本级目录除外
|
||||||
if (false == ".".equals(name) && false == "..".equals(name)) {
|
if (!".".equals(name) && !"..".equals(name)) {
|
||||||
delDir(childPath);
|
delDir(childPath);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -557,7 +558,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
|
|
||||||
if (StrUtil.isNotBlank(destPath)) {
|
if (StrUtil.isNotBlank(destPath)) {
|
||||||
mkDirs(destPath);
|
mkDirs(destPath);
|
||||||
if (false == cd(destPath)) {
|
if (!cd(destPath)) {
|
||||||
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", destPath);
|
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", destPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -573,6 +574,54 @@ public class Ftp extends AbstractFtp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件或目录(包含当前及子孙目录的所有文件)
|
||||||
|
*
|
||||||
|
* @param destPath 目标路径
|
||||||
|
* @param uploadFile 上传文件或目录
|
||||||
|
*/
|
||||||
|
public void uploadFileOrDirectory(String destPath, final File uploadFile) {
|
||||||
|
if (uploadFile.isFile()) {
|
||||||
|
this.upload(destPath, uploadFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.mkDirs(destPath);
|
||||||
|
recursiveUpload(destPath, uploadFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 递归上传文件(支持目录)
|
||||||
|
*
|
||||||
|
* @param destPath 目录路径
|
||||||
|
* @param uploadFile 上传文件或目录
|
||||||
|
*/
|
||||||
|
public void recursiveUpload(String destPath, final File uploadFile) {
|
||||||
|
if (uploadFile.isFile()) {
|
||||||
|
this.upload(destPath, uploadFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
File[] files = uploadFile.listFiles();
|
||||||
|
if (Objects.isNull(files)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//第一次只处理文件,防止目录在前面导致先处理子孙目录,而引发文件所在目录不正确
|
||||||
|
for (File f : files) {
|
||||||
|
if (f.isFile()) {
|
||||||
|
this.upload(destPath, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//第二次只处理目录
|
||||||
|
for (File f : files) {
|
||||||
|
if (f.isDirectory()) {
|
||||||
|
destPath = destPath + File.separator + f.getName();
|
||||||
|
this.mkDirs(destPath);
|
||||||
|
recursiveUpload(destPath, f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
*
|
*
|
||||||
@ -602,9 +651,9 @@ public class Ftp extends AbstractFtp {
|
|||||||
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
|
srcFile = StrUtil.format("{}/{}", sourcePath, fileName);
|
||||||
destFile = FileUtil.file(destDir, fileName);
|
destFile = FileUtil.file(destDir, fileName);
|
||||||
|
|
||||||
if (false == ftpFile.isDirectory()) {
|
if (!ftpFile.isDirectory()) {
|
||||||
// 本地不存在文件或者ftp上文件有修改则下载
|
// 本地不存在文件或者ftp上文件有修改则下载
|
||||||
if (false == FileUtil.exist(destFile)
|
if (!FileUtil.exist(destFile)
|
||||||
|| (ftpFile.getTimestamp().getTimeInMillis() > destFile.lastModified())) {
|
|| (ftpFile.getTimestamp().getTimeInMillis() > destFile.lastModified())) {
|
||||||
download(srcFile, destFile);
|
download(srcFile, destFile);
|
||||||
}
|
}
|
||||||
@ -628,7 +677,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
if (outFile.isDirectory()) {
|
if (outFile.isDirectory()) {
|
||||||
outFile = new File(outFile, fileName);
|
outFile = new File(outFile, fileName);
|
||||||
}
|
}
|
||||||
if (false == outFile.exists()) {
|
if (!outFile.exists()) {
|
||||||
FileUtil.touch(outFile);
|
FileUtil.touch(outFile);
|
||||||
}
|
}
|
||||||
try (OutputStream out = FileUtil.getOutputStream(outFile)) {
|
try (OutputStream out = FileUtil.getOutputStream(outFile)) {
|
||||||
@ -665,7 +714,7 @@ public class Ftp extends AbstractFtp {
|
|||||||
pwd = pwd();
|
pwd = pwd();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false == cd(path)) {
|
if (!cd(path)) {
|
||||||
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
|
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +33,15 @@ public class FtpTest {
|
|||||||
IoUtil.close(ftp);
|
IoUtil.close(ftp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void uploadDirectorTest() {
|
||||||
|
final Ftp ftp = new Ftp("localhost");
|
||||||
|
|
||||||
|
ftp.uploadFileOrDirectory("/temp", FileUtil.file("d:/test/"));
|
||||||
|
IoUtil.close(ftp);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void reconnectIfTimeoutTest() throws InterruptedException {
|
public void reconnectIfTimeoutTest() throws InterruptedException {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user