用java scp写一个 将本机的文件夹传到另外一台服务器上并创建文件夹
时间: 2023-05-12 17:03:40 浏览: 119
可以使用以下代码实现:
```
import com.jcraft.jsch.*;
public class ScpExample {
public static void main(String[] args) {
String user = "username";
String password = "password";
String host = "remote.host.com";
int port = 22;
String localFilePath = "/path/to/local/folder";
String remoteFilePath = "/path/to/remote/folder";
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.mkdir(remoteFilePath);
sftpChannel.put(localFilePath, remoteFilePath);
sftpChannel.exit();
session.disconnect();
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
}
```
其中,`user`、`password`、`host`、`port`、`localFilePath`、`remoteFilePath`都需要根据实际情况进行修改。
阅读全文