Sftp java类的应用(包括密钥连接)

Sftp java类的应用(包括密钥连接)

使用:jsch-0.1.5.jar
/**
*
*/
package com.zgs.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
*
* @author Administrator
* 2015-4-10
*
*/
public class SFtp {

private String userName;
private String password;
private int port;
private String hostName;
private String priKeyFile;
private String passphrase;

Session session=null;
Channel channel=null;

/**
* @param userName
* @param password
* @param port
* @param hostName
*/
public SFtp(String userName, String password, int port, String hostName) {
super();
this.userName = userName;
this.password = password;
this.port = port;
this.hostName = hostName;
}
/**
* @param userName
* @param password
* @param port
* @param hostName
* @param priKeyFile
* @param passphrase
*/
public SFtp(String userName, String password, int port, String hostName,
String priKeyFile, String passphrase) {
super();
this.userName = userName;
this.password = password;
this.port = port;
this.hostName = hostName;
this.priKeyFile = priKeyFile;
this.passphrase = passphrase;
}

public ChannelSftp connect(){

JSch jsch=new JSch();
try {
if(port>0){
session=jsch.getSession(userName, hostName, port);
}else{
session=jsch.getSession(userName, hostName);
}
if (session == null) {
return null;
//throw new Exception("session is null");
}
//设置登陆主机的密码
session.setPassword(password);//设置密码
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.setTimeout(20000);
session.connect();
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect();
return (ChannelSftp) channel;
} catch (JSchException e) {
// TODO Auto-generated catch block
return null;
}
}

/**
* 密钥文件连接
* @return
*/

public ChannelSftp priKeyConnect(){
JSch jsch=new JSch();
try {
if(priKeyFile !=null && !"".equals(priKeyFile)){
if(passphrase !=null && !"".equals(passphrase)){
jsch.addIdentity(priKeyFile, passphrase);
}else{
jsch.addIdentity(priKeyFile);
}
}
if(port >0){
session=jsch.getSession(userName, hostName, port);
}else{
session=jsch.getSession(userName, hostName);
}
Properties config=new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
//session.setTimeout(20000);
session.connect();
channel=session.openChannel("sftp");
channel.connect();
return (ChannelSftp)channel;
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}


/**
*
* @param is 要上传的本地文件流
* @param dsc 目标目录或文件,若是目录则上传的目标文件名和本地文件名一样
* @param sftp
* @return
*/
public boolean upload(InputStream is,String dsc,ChannelSftp sftp){
try {
sftp.put(is, dsc);
return true;
} catch (SftpException e) {
// TODO Auto-generated catch block
return false;
}
}

/**
*
* @param remotedir 要上传到的目录
* @param uploadFile 上传的文件
* @param sftp
* @return
*/
public boolean upload(String remotedir, String uploadFile, ChannelSftp sftp){
try {
sftp.cd(remotedir);
File file=new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
}


/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @param sftp
*/
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.get(downloadFile,saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}

public void disconnected(ChannelSftp sftp){
if (sftp != null) {
try {
sftp.getSession().disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
sftp.disconnect();
}
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值