- 从本地服务器指定目录推送到静态资源服务器指定目录下,删除原文件
public static void uploadFile(String localFullFileName,String remotePath, SftpConfig sftpConfig){
SftpClientWrapper sftp = new SftpClientWrapper();
try {
if (StringUtils.isNotEmpty(sftpConfig.getPassword())){
sftp.initSession(sftpConfig.getIp(), sftpConfig.getPort(), sftpConfig.getUsername(),sftpConfig.getPassword());
} else if (StringUtils.isNotEmpty(sftpConfig.getPrivateKey())) {
sftp.initSessionByKey(sftpConfig.getIp(),sftpConfig.getPort(),sftpConfig.getUsername(),sftpConfig.getPrivateKey());
}else {
throw new SftpException(4,"sftp配置错误");
}
sftp.initChannelSftp();
if (StringUtils.isNotEmpty(remotePath)){
sftp.sftp.cd(remotePath);
}
log.debug(sftp.sftp.pwd());
sftp.upload(sLocalFullFileName,"./");
} catch (JSchException | SftpException e) {
throw new RuntimeException(e);
}finally {
sftp.close();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27