add method

This commit is contained in:
Looly 2021-10-26 22:35:04 +08:00
parent a77dce46cd
commit 32684b2d5c
4 changed files with 52 additions and 9 deletions

View File

@ -12,10 +12,12 @@
* 【core 】 MailAccount中charset增加null时的默认规则
* 【core 】 NumberUtil.compare修正注释说明issue#I4FAJ1@Gitee
* 【core 】 增加RFC3986类
* 【extra 】 Sftp增加put和upload重载issue#I4FGDH@Gitee
### 🐞Bug修复
* 【core 】 修复UrlBuilder.addPath歧义问题issue#1912@Github
* 【core 】 修复StrBuilder中总长度计算问题issue#I4F9L7@Gitee
* 【core 】 修复CharSequenceUtil.wrapIfMissing预定义长度计算问题issue#I4FDZ2@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -2857,10 +2857,10 @@ public class CharSequenceUtil {
len += str.length();
}
if (isNotEmpty(prefix)) {
len += str.length();
len += prefix.length();
}
if (isNotEmpty(suffix)) {
len += str.length();
len += suffix.length();
}
StringBuilder sb = new StringBuilder(len);
if (isNotEmpty(prefix) && false == startWith(str, prefix)) {

View File

@ -489,9 +489,9 @@ public class Ftp extends AbstractFtp {
* 上传文件到指定目录可选
*
* <pre>
* 1. path为null或""上传到当前路径
* 2. path为相对路径则相对于当前路径的子路径
* 3. path为绝对路径则上传到此路径
* 1. destPath为null或""上传到当前路径
* 2. destPath为相对路径则相对于当前路径的子路径
* 3. destPath为绝对路径则上传到此路径
* </pre>
*
* @param file 文件
@ -512,9 +512,9 @@ public class Ftp extends AbstractFtp {
* 上传文件到指定目录可选
*
* <pre>
* 1. path为null或""上传到当前路径
* 2. path为相对路径则相对于当前路径的子路径
* 3. path为绝对路径则上传到此路径
* 1. destPath为null或""上传到当前路径
* 2. destPath为相对路径则相对于当前路径的子路径
* 3. destPath为绝对路径则上传到此路径
* </pre>
*
* @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径

View File

@ -17,6 +17,7 @@ import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.SftpProgressMonitor;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
@ -337,7 +338,7 @@ public class Sftp extends AbstractFtp {
try {
sftpATTRS = this.channel.stat(dir);
} catch (SftpException e) {
if(e.getMessage().contains("No such file")){
if (e.getMessage().contains("No such file")) {
// 文件不存在直接返回false
// pr#378@Gitee
return false;
@ -464,6 +465,27 @@ public class Sftp extends AbstractFtp {
return true;
}
/**
* 上传文件到指定目录可选
*
* <pre>
* 1. path为null或""上传到当前路径
* 2. path为相对路径则相对于当前路径的子路径
* 3. path为绝对路径则上传到此路径
* </pre>
*
* @param destPath 服务端路径可以为{@code null} 或者相对路径或绝对路径
* @param fileName 文件名
* @param fileStream 文件流
* @return 是否上传成功
* @since 5.7.16
*/
public boolean upload(String destPath, String fileName, InputStream fileStream) {
destPath = StrUtil.addSuffixIfNot(destPath, StrUtil.SLASH) + StrUtil.removePrefix(fileName, StrUtil.SLASH);
put(fileStream, destPath, null, Mode.OVERWRITE);
return true;
}
/**
* 将本地文件上传到目标服务器目标文件名为destPath若destPath为目录则目标文件名将与srcFilePath文件名相同覆盖模式
*
@ -506,6 +528,25 @@ public class Sftp extends AbstractFtp {
return this;
}
/**
* 将本地数据流上传到目标服务器目标文件名为destPath目标必须为文件
*
* @param srcStream 本地的数据流
* @param destPath 目标路径
* @param monitor 上传进度监控通过实现此接口完成进度显示
* @param mode {@link Mode} 模式
* @return this
* @since 5.7.16
*/
public Sftp put(InputStream srcStream, String destPath, SftpProgressMonitor monitor, Mode mode) {
try {
channel.put(srcStream, destPath, monitor, mode.ordinal());
} catch (SftpException e) {
throw new JschRuntimeException(e);
}
return this;
}
@Override
public void download(String src, File destFile) {
get(src, FileUtil.getAbsolutePath(destFile));