sftp工具类

import java.io.File;
import java.io.FileInputStream;
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;

public class SftpUtil {

private static SftpUtil sftpUtil = new SftpUtil();
protected String host = "******";// sftp服务器ip
protected String username = "***";// 用户名
protected String password = "******";// 密码
protected String privateKey = "D:/sftp键值对/***.key";// 密钥文件路径
protected String passphrase = "";// 密钥口令
protected int port = 22;// 默认的sftp端口号是22

public static void main(String[] args) {
SftpUtil t = new SftpUtil();
ChannelSftp sftp = t.connectSFTP();
System.out.println(123);
// t.download("download", "", "");
// t.upload("/", "e:/data4.txt", sftp);
t.download("/", "HJB01_file_index_20140723_1.zip", System.getProperty("user.dir"), sftp);
t.disconnected(sftp);
}

public SftpUtil(){};

public static SftpUtil getInstance(){
return sftpUtil;
}

/**
* 获取连接
*
* @return channel
*/
public ChannelSftp connectSFTP() {
JSch jsch = new JSch();
Channel channel = null;
try {
if (privateKey != null && !"".equals(privateKey)) {
// 使用密钥验证方式,密钥可以使有口令的密钥,也可以是没有口令的密钥
if (passphrase != null && !"".equals(passphrase)) {
jsch.addIdentity(privateKey, passphrase);
} else {
jsch.addIdentity(privateKey);
}
}
Session session = jsch.getSession(username, host, port);
if (password != null && !"".equals(password)) {
session.setPassword(password);
}
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");// do not verify host
// key
session.setConfig(sshConfig);
session.setTimeout(10000);
//session.setServerAliveInterval(10000);
session.connect();
// 参数sftp指明要打开的连接是sftp连接
channel = session.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
}
return (ChannelSftp) channel;
}

/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 下载文件
*
* @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();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值