mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
sftp 新增将本地文件或者文件夹同步上传到远程目录的方法
This commit is contained in:
parent
81c7bb4b46
commit
0c730e249c
@ -17,6 +17,7 @@ import com.jcraft.jsch.SftpException;
|
|||||||
import com.jcraft.jsch.SftpProgressMonitor;
|
import com.jcraft.jsch.SftpProgressMonitor;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -319,7 +320,7 @@ public class Sftp extends AbstractFtp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mkdir(String dir) {
|
public boolean mkdir(String dir) {
|
||||||
if(isDir(dir)){
|
if (isDir(dir)) {
|
||||||
// 目录已经存在,创建直接返回
|
// 目录已经存在,创建直接返回
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -332,7 +333,7 @@ public class Sftp extends AbstractFtp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDir(String dir){
|
public boolean isDir(String dir) {
|
||||||
final SftpATTRS sftpATTRS;
|
final SftpATTRS sftpATTRS;
|
||||||
try {
|
try {
|
||||||
sftpATTRS = this.channel.stat(dir);
|
sftpATTRS = this.channel.stat(dir);
|
||||||
@ -423,6 +424,35 @@ public class Sftp extends AbstractFtp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将本地文件或者文件夹同步(覆盖)上传到远程路径
|
||||||
|
*
|
||||||
|
* @param file 文件或者文件夹
|
||||||
|
* @param remotePath 远程路径
|
||||||
|
*/
|
||||||
|
public void syncPath(File file, String remotePath) {
|
||||||
|
if (!FileUtil.exist(file)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
if (files == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (File fileItem : files) {
|
||||||
|
if (fileItem.isDirectory()) {
|
||||||
|
String mkdir = FileUtil.normalize(remotePath + "/" + fileItem.getName());
|
||||||
|
this.syncPath(fileItem, mkdir);
|
||||||
|
} else {
|
||||||
|
this.syncPath(fileItem, remotePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.mkDirs(remotePath);
|
||||||
|
this.upload(remotePath, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean upload(String destPath, File file) {
|
public boolean upload(String destPath, File file) {
|
||||||
put(FileUtil.getAbsolutePath(file), destPath);
|
put(FileUtil.getAbsolutePath(file), destPath);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user