在java中使用jsch连接linux并上传文件
废话不多说,上代码
try{
ChannelSftp sftp;
JSch jsch = new JSch();
// 此处使用的是密钥对登录
jsch.addIdentity(publicKey);
Session session = jsch.getSession("root","hostIp",22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
// 如想使用密码登录则使用 session.setPassword();
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
sftp.cd(destination);
sftp.put(is,fileName);
sftp.disconnect();
sftp.getSession().disconnect();
} catch (Exception e){
e.printStackTrace();
}
pom.xml
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>