本文实例为大家分享了java使用JSCH实现SFTP文件管理的具体代码,供大家参考,具体内容如下
一、连接配置
1.在项目中导入jsch-0.1.51.jar包;
2.创建SFTP类,存放连接属性,其中要注意一点,在进行FTP操作时,一个会话在建立连接通道后进入A目录进行文件操作,不能直接跳到同级或上级目录操作,需要先退出当前会话或者新建会话。
public class SFTP{
private Session session;//会话
private Channel channel;//连接通道
private ChannelSftp sftp;// sftp操作类
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
public ChannelSftp getSftp() {
return sftp;
}
public void setSftp(ChannelSftp sftp) {
this.sftp = sftp;
}
}
3.创建SFTPUtil类,创建连接配置方法
public static void getConnect(SFTP s) throws Exception {
// String privateKey ="key";
//
// String passphrase ="path";
String host ="127.0.0.1";
int port =22;
String username ="test";
String password ="test";
Session session = null;
Channel channel = null;
ChannelSftp sftp = null;// sftp操作类
JSch jsch = new JSch();
//设置密钥和密码
//支持密钥的方式登陆,只需在jsch.getSession之前设置一下密钥的相关信息就可以了
// if (privateKey != null && !"".equals(privateKey)) {
// if (passphrase != null && "".equals(passphrase)) {
// //设置带口令的密钥
// jsch.addIdentity(privateKey, passphrase);
// } else {
// //设置不带口令的密钥
// jsch.addIdentity(privateKey);
// }
// }
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no"); // 不验证 HostKey
session.setConfig(config);
try {
session.connect();
} catch (Exception e) {
if (session.isConnected())
session.disconnect();
log.error("连接服务器失败,请检查主机[" + host + "],端口[" + port
+ "],用户名[" + username + "],端口[" + port
+ "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
}
channel = session.openChannel("sftp");
try {
channel.connect();
} catch (Exception e) {
if (channel.isConnected())
channel.disconnect();
log.error("连接服务器失败,请检查主机[" + host + "],端口[" + port
+ "],用户名[" + username + "],密码是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
}
sftp = (ChannelSftp) channel;
s.setChannel(channel);
s.setSession(session);
s.setSftp(sftp);
}
5.关闭连接方法
public static void disConn(Session session,Channel channel,ChannelSftp sftp)throws Exception{
if(null != sftp){
sftp.disconnect();
sftp.exit();
sftp = null;
}
if(null != channel){
channel.disconnect();
channel = null;
}
if(null != session){
session.disconnect();
session = null;
}
}
二、SFTP目录、文件操作管理
1.上传文件
public static void upload(String directory,String uploadFile) throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
try{
sftp.cd(directory); //进入目录
}catch(SftpException sException){
if(sftp.SSH_FX_NO_SUCH_FILE == sException.id){ //指定上传路径不存在
sftp.mkdir(directory);//创建目录
sftp.cd(directory); //进入目录
}
}
}
File file = new File(uploadFile);
InputStream in= new FileInputStream(file);
sftp.put(in, file.getName());
in.close();
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
2.文件下载
public static void download(String directory, String downloadFile,String saveFile) throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
sftp.cd(directory); //进入目录
File file = new File(saveFile);
boolean bFile;
bFile = false;
bFile = file.exists();
if (!bFile) {
bFile = file.mkdirs();//创建目录
}
OutputStream out=new FileOutputStream(new File(saveFile,downloadFile));
sftp.get(downloadFile, out);
out.flush();
out.close();
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
3.删除文件
public static void delete(String directory, String deleteFile) throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
sftp.cd(directory); //进入的目录应该是要删除的目录的上一级
sftp.rm(deleteFile);//删除目录
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
4.列出目录下的文件
public static ListlistFiles(String directory) throws Exception { SFTP s=new SFTP(); getConnect(s);//建立连接 Session session = s.getSession(); Channel channel = s.getChannel(); ChannelSftp sftp = s.getSftp();// sftp操作类 Vector fileList=null; List fileNameList = new ArrayList (); fileList = sftp.ls(directory); //返回目录下所有文件名称 disConn(session,channel,sftp); Iterator it = fileList.iterator(); while(it.hasNext()) { String fileName = ((LsEntry)it.next()).getFilename(); if(".".equals(fileName) || "..".equals(fileName)){ continue; } fileNameList.add(fileName); } return fileNameList; }
5.删除目录下所有文件
public static void deleteAllFile(String directory) throws Exception{
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
List files=listFiles(directory);//返回目录下所有文件名称
sftp.cd(directory); //进入目录
for (String deleteFile : files) {
sftp.rm(deleteFile);//循环一次删除目录下的文件
}
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
6.删除目录 (删除的目录必须为空)
public static void deleteDir(String deleteDir) throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
sftp.rmdir(deleteDir);
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
7.创建目录
public static void creatDir(String directory,String dir) throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
sftp.cd(directory);
sftp.mkdir(dir);
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
8.更改文件名
public static void rename(String directory, String oldFileNm, String newFileNm) throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
sftp.cd(directory);
sftp.rename(oldFileNm, newFileNm);
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
9.进入目录
public static void cd(String directory)throws Exception {
SFTP s=new SFTP();
getConnect(s);//建立连接
Session session = s.getSession();
Channel channel = s.getChannel();
ChannelSftp sftp = s.getSftp();// sftp操作类
try {
sftp.cd(directory); //目录要一级一级进
} catch (Exception e) {
throw new Exception(e.getMessage(),e);
} finally {
disConn(session,channel,sftp);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



