java sftp上传jar,并删除服务器的jar
时间: 2023-10-28 11:22:00 浏览: 153
您可以使用JSch库来实现Java SFTP文件上传和删除。以下是一个示例代码,可以上传Jar文件并删除服务器上的旧Jar文件:
```java
import com.jcraft.jsch.*;
import java.io.File;
public class SftpUploadAndDelete {
public static void main(String[] args) {
String hostname = "hostname";
int port = 22;
String username = "username";
String password = "password";
String localFilePath = "localFilePath";
String remoteDirectoryPath = "remoteDirectoryPath";
String remoteJarFileName = "remoteJarFileName";
JSch jsch = new JSch();
try {
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
// 上传新的Jar文件
channelSftp.put(localFilePath, remoteDirectoryPath + remoteJarFileName);
// 删除旧的Jar文件
String oldJarFilePath = remoteDirectoryPath + getLatestJarFileName(channelSftp, remoteDirectoryPath, remoteJarFileName);
channelSftp.rm(oldJarFilePath);
channelSftp.disconnect();
session.disconnect();
System.out.println("Jar file uploaded and old jar file deleted successfully.");
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
private static String getLatestJarFileName(ChannelSftp channelSftp, String remoteDirectoryPath, String remoteJarFileName) throws SftpException {
String latestJarFileName = "";
SftpATTRS attrs = null;
String[] files = channelSftp.ls(remoteDirectoryPath);
for (String file : files) {
if (file.startsWith(remoteJarFileName) && file.endsWith(".jar")) {
if (attrs == null) {
attrs = channelSftp.stat(remoteDirectoryPath + file);
latestJarFileName = file;
} else {
SftpATTRS newAttrs = channelSftp.stat(remoteDirectoryPath + file);
if (newAttrs.getMTime() > attrs.getMTime()) {
attrs = newAttrs;
latestJarFileName = file;
}
}
}
}
return latestJarFileName;
}
}
```
您需要将上面的代码中的以下变量替换为您的实际值:
- hostname:SFTP服务器的主机名。
- port:SFTP服务器的端口号。
- username:SFTP服务器的用户名。
- password:SFTP服务器的密码。
- localFilePath:本地Jar文件的路径。
- remoteDirectoryPath:远程目录的路径。
- remoteJarFileName:远程Jar文件的名称。
代码将上传新的Jar文件,然后查找并删除远程目录中旧的Jar文件。删除旧Jar文件的方法是通过获取远程目录中所有以指定名称开头且以“.jar”结尾的文件列表,然后比较它们的修改时间,找到最新的Jar文件并删除。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)