首先,需要jar包:jsch-0.1.54.jar
然后就只需要这一个类(里面有个获取连接信息并打印到console的内部类,方便出错的时候看看是什么原因连接不上)
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 String host;//sftp服务器ip
private String username;//用户名
private String password;//密码
private String privateKey;//密钥文件路径
private String passphrase;//密钥口令
private int port = 9022;//默认的sftp端口号9022
public SFTPUtil(String host,String username,String password,String privateKey, String passphrase, int port) {
this.host = host;
this.username = username;
this.password = password;
this.privateKey = privateKey;
this.passphrase = passphrase;
this.port = port;
}
/**
* 获取连接
* @return channel
*/
public ChannelSftp connectSFTP() {
JSch jsch = new JSch();
Channel channel = null;
System.out.println("--------privateKey:"+privateKey);
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);
}
//Console 里面打印连接的更多相关信息
com.jcraft.jsch.Logger logger = new SettleLogger();
JSch.setLogger(logger);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");// do not verify host key
session.setConfig(sshConfig);
// session.setTimeout(timeout);
session.setServerAliveInterval(92000);
session.connect();
//参数sftp指明要打开的连接是sftp连接
channel = session.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
System.out.println("connectSFTP:"+e);
}
return (ChannelSftp) channel;
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
System.out.println(directory);
sftp.cd(directory);
System.out.println(uploadFile);
File file = new File(uploadFile);
System.out.println(directory+","+uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
System.out.println("upload:"+e);
}
}
/**
* 下载文件
*
* @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) {
System.out.println("download:"+e);
}
}
/**
* 删除文件
*
* @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) {
System.out.println("delete:"+e);
}
}
public void disconnected(ChannelSftp sftp){
if (sftp != null) {
try {
sftp.getSession().disconnect();
} catch (JSchException e) {
System.out.println("disconnected:"+e);
}
sftp.disconnect();
}
}
}
//Console 里面打印连接的更多相关信息
class SettleLogger implements com.jcraft.jsch.Logger {
public boolean isEnabled(int level) {
return true;
}
public void log(int level, String msg) {
System.out.println(msg);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
至于调用 都是直接按着来写的
//------------------------connect sftp------------------------
String host="sftp连接的ip";
String username = "sftp连接的用户名";
String password = "sftp连接的密码";
String privateKey = "ssh 私钥本地路径";
String passphrase = "";//ssh 私钥口令
int port = 22;//默认端口号是22
String directory = "./";//默认地址
String uploadfilepath = "需要上传的文件的本地路径";
//------------------------connect sftp------------------------
SFTPUtil sftpUtil = new SFTPUtil(host,username,password,privateKey,passphrase,port);
ChannelSftp channelsftp = sftpUtil.connectSFTP();
if(channelsftp!=null) {
sftpUtil.upload(directory,uploadfilepath,channelsftp);
//下载和删除就不写了 反正都是写一下服务器的文件路径 需要操作的文件 最后再写个channelsftp就好了
sftpUtil.disconnected(channelsftp);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
转自:https://blog.csdn.net/qq_22778717/article/details/78751123