add methods

This commit is contained in:
Looly 2022-09-26 18:37:31 +08:00
parent 8b9b610ba3
commit 7db4d93268
6 changed files with 81 additions and 38 deletions

View File

@ -195,7 +195,7 @@ public abstract class AbstractFtp implements Closeable {
* @param file 需要上传的文件
* @return 是否成功
*/
public abstract boolean upload(String destPath, File file);
public abstract boolean uploadFile(String destPath, File file);
/**
* 下载文件

View File

@ -269,10 +269,11 @@ public class Ftp extends AbstractFtp {
/**
* 是否执行完操作返回当前目录
*
* @return 执行完操作是否返回当前目录
* @since 5.7.17
*/
public boolean isBackToPwd(){
public boolean isBackToPwd() {
return this.backToPwd;
}
@ -340,7 +341,7 @@ public class Ftp extends AbstractFtp {
* 遍历某个目录下所有文件和目录不会递归遍历<br>
* 此方法自动过滤"."".."两种目录
*
* @param path 目录
* @param path 目录
* @param predicate 过滤器null表示不过滤默认去掉"."".."两种目录
* @return 文件或目录列表
* @since 5.3.5
@ -408,6 +409,7 @@ public class Ftp extends AbstractFtp {
*
* @param path 路径
* @return 状态int服务端不同返回不同
* @throws IORuntimeException IO异常
* @since 5.4.3
*/
public int stat(final String path) throws IORuntimeException {
@ -491,39 +493,42 @@ public class Ftp extends AbstractFtp {
* 上传文件到指定目录可选
*
* <pre>
* 1. destPath为null或""上传到当前路径
* 2. destPath为相对路径则相对于当前路径的子路径
* 3. destPath为绝对路径则上传到此路径
* 1. remotePath为null或""上传到当前路径
* 2. remotePath为相对路径则相对于当前路径的子路径
* 3. remotePath为绝对路径则上传到此路径
* </pre>
*
* @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param file 文件
* @param remotePath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param file 文件
* @return 是否上传成功
*/
@Override
public boolean upload(final String destPath, final File file) {
public boolean uploadFile(final String remotePath, final File file) {
Assert.notNull(file, "file to upload is null !");
return upload(destPath, file.getName(), file);
if (false == FileUtil.isFile(file)) {
throw new FtpException("[{}] is not a file!", file);
}
return uploadFile(remotePath, file.getName(), file);
}
/**
* 上传文件到指定目录可选
*
* <pre>
* 1. destPath为null或""上传到当前路径
* 2. destPath为相对路径则相对于当前路径的子路径
* 3. destPath为绝对路径则上传到此路径
* 1. remotePath为null或""上传到当前路径
* 2. remotePath为相对路径则相对于当前路径的子路径
* 3. remotePath为绝对路径则上传到此路径
* </pre>
*
* @param file 文件
* @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param fileName 自定义在服务端保存的文件名
* @param file 文件
* @param remotePath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param fileName 自定义在服务端保存的文件名
* @return 是否上传成功
* @throws IORuntimeException IO异常
*/
public boolean upload(final String destPath, final String fileName, final File file) throws IORuntimeException {
public boolean uploadFile(final String remotePath, final String fileName, final File file) throws IORuntimeException {
try (final InputStream in = FileUtil.getInputStream(file)) {
return upload(destPath, fileName, in);
return uploadFile(remotePath, fileName, in);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
@ -533,18 +538,18 @@ public class Ftp extends AbstractFtp {
* 上传文件到指定目录可选
*
* <pre>
* 1. destPath为null或""上传到当前路径
* 2. destPath为相对路径则相对于当前路径的子路径
* 3. destPath为绝对路径则上传到此路径
* 1. remotePath为null或""上传到当前路径
* 2. remotePath为相对路径则相对于当前路径的子路径
* 3. remotePath为绝对路径则上传到此路径
* </pre>
*
* @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param remotePath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param fileName 文件名
* @param fileStream 文件流
* @return 是否上传成功
* @throws IORuntimeException IO异常
*/
public boolean upload(final String destPath, final String fileName, final InputStream fileStream) throws IORuntimeException {
public boolean uploadFile(final String remotePath, final String fileName, final InputStream fileStream) throws IORuntimeException {
try {
client.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (final IOException e) {
@ -556,10 +561,10 @@ public class Ftp extends AbstractFtp {
pwd = pwd();
}
if (StrUtil.isNotBlank(destPath)) {
mkDirs(destPath);
if (false == cd(destPath)) {
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", destPath);
if (StrUtil.isNotBlank(remotePath)) {
mkDirs(remotePath);
if (false == cd(remotePath)) {
throw new FtpException("Change dir to [{}] error, maybe dir not exist!", remotePath);
}
}
@ -574,6 +579,41 @@ public class Ftp extends AbstractFtp {
}
}
/**
* 递归上传文件支持目录<br>
* 上传时如果uploadFile为目录只复制目录下所有目录和文件到目标路径下并不会复制目录本身<br>
* 上传时自动创建父级目录
*
* @param remotePath 目录路径
* @param uploadFile 上传文件或目录
*/
public void upload(final String remotePath, final File uploadFile) {
if (false == FileUtil.isDirectory(uploadFile)) {
this.uploadFile(remotePath, uploadFile);
return;
}
final File[] files = uploadFile.listFiles();
if (ArrayUtil.isEmpty(files)) {
return;
}
final List<File> dirs = new ArrayList<>(files.length);
//第一次只处理文件防止目录在前面导致先处理子目录而引发文件所在目录不正确
for (final File f : files) {
if (f.isDirectory()) {
dirs.add(f);
} else {
this.uploadFile(remotePath, f);
}
}
//第二次只处理目录
for (final File f : dirs) {
final String dir = FileUtil.normalize(remotePath + "/" + f.getName());
upload(dir, f);
}
}
/**
* 下载文件
*

View File

@ -473,11 +473,11 @@ public class Sftp extends AbstractFtp {
/**
* 将本地文件或者文件夹同步覆盖上传到远程路径
*
* @param file 文件或者文件夹
* @param remotePath 远程路径
* @param file 文件或者文件夹
* @since 5.7.6
*/
public void syncUpload(final File file, final String remotePath) {
public void upload(final String remotePath, final File file) {
if (false == FileUtil.exist(file)) {
return;
}
@ -489,19 +489,22 @@ public class Sftp extends AbstractFtp {
for (final File fileItem : files) {
if (fileItem.isDirectory()) {
final String mkdir = FileUtil.normalize(remotePath + "/" + fileItem.getName());
this.syncUpload(fileItem, mkdir);
this.upload(mkdir, fileItem);
} else {
this.syncUpload(fileItem, remotePath);
this.uploadFile(remotePath, fileItem);
}
}
} else {
this.mkDirs(remotePath);
this.upload(remotePath, file);
this.uploadFile(remotePath, file);
}
}
@Override
public boolean upload(final String destPath, final File file) {
public boolean uploadFile(final String destPath, final File file) {
if(false == FileUtil.isFile(file)){
throw new FtpException("[{}] is not a file!", file);
}
this.mkDirs(destPath);
put(FileUtil.getAbsolutePath(file), destPath);
return true;
}
@ -521,7 +524,7 @@ public class Sftp extends AbstractFtp {
* @return 是否上传成功
* @since 5.7.16
*/
public boolean upload(String destPath, final String fileName, final InputStream fileStream) {
public boolean uploadFile(String destPath, final String fileName, final InputStream fileStream) {
destPath = StrUtil.addSuffixIfNot(destPath, StrUtil.SLASH) + StrUtil.removePrefix(fileName, StrUtil.SLASH);
put(fileStream, destPath, null, Mode.OVERWRITE);
return true;

View File

@ -183,7 +183,7 @@ public class SshjSftp extends AbstractFtp {
}
@Override
public boolean upload(final String destPath, final File file) {
public boolean uploadFile(final String destPath, final File file) {
try {
sftp.put(new FileSystemFile(file), destPath);
return containsFile(destPath);

View File

@ -27,7 +27,7 @@ public class FtpTest {
public void uploadTest() {
final Ftp ftp = new Ftp("localhost");
final boolean upload = ftp.upload("/temp", FileUtil.file("d:/test/test.zip"));
final boolean upload = ftp.uploadFile("/temp", FileUtil.file("d:/test/test.zip"));
Console.log(upload);
IoUtil.close(ftp);

View File

@ -42,7 +42,7 @@ public class SftpTest {
@Test
@Ignore
public void uploadTest() {
sshjSftp.upload("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME"));
sshjSftp.uploadFile("/home/test/temp/", new File("C:\\Users\\akwangl\\Downloads\\temp\\辽宁_20190718_104324.CIME"));
}
@Test