java 代码发送jar包到服务器指定文件夹
时间: 2023-11-26 11:02:42 浏览: 33
要实现将本地的jar包发送到服务器的指定文件夹,可以使用Java中的FTP客户端库,例如Apache Commons Net或Java FTP Client。下面是使用Apache Commons Net实现将jar包发送到服务器指定文件夹的示例代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FtpClientExample {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String user = "username";
String password = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteDirPath = "/path/to/remote/dir/";
String localJarFilePath = "/path/to/local/jar/file.jar";
File localFile = new File(localJarFilePath);
String remoteFileName = localFile.getName();
String remoteFilePath = remoteDirPath + remoteFileName;
FileInputStream inputStream = new FileInputStream(localFile);
System.out.println("Start uploading file");
boolean done = ftpClient.storeFile(remoteFilePath, inputStream);
inputStream.close();
if (done) {
System.out.println("The file is uploaded successfully.");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
```
这段代码通过FTP连接到服务器,设置传输模式为二进制模式,然后将本地的jar包上传到远程服务器的指定文件夹中。可以根据需要修改代码实现其他FTP操作。