目录jsch是什么?怎么使用?添加jar依赖Jsch是什么? JSch 是ssh2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你
JSch 是ssh2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器
Jsch功能很强大,博主这里主要用来做文件操作
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
我把我的SftpUtil贴下面了,注释还算清楚
public class SftpUtil {
public static void sftpFileUpload(String ip,int port, String user, String psw, String fielPath,String serverFileName,InputStream instream) throws Exception {
Session session =getSession( ip, user, psw, port);
Channel channel = null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd(fielPath);
OutputStream outstream = sftp.put(serverFileName);
byte b[] = new byte[1024*200];//每次传输200k
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
outstream.flush();
outstream.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
}
public static void sftpFileDownload(String ip,int port, String user, String psw, String fielPath,String serverFileName,OutputStream outputStream) throws Exception {
Session session =getSession( ip, user, psw, port);
Channel channel = null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd(fielPath);
sftp.get(serverFileName,outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
}
public static void sftpFileDelete(String ip,int port, String user, String psw, String fielPath,String serverFileName) throws Exception {
Session session =getSession( ip, user, psw, port);
Channel channel = null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd(fielPath);
sftp.rm(serverFileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
}
public static Vector seeServerFileList(String ip, int port,String user, String psw, String fielPath)throws Exception{
Session session =getSession( ip, user, psw, port);
Channel channel = null;
Vector v=null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd(fielPath);
//列出服务器指定的文件列表
v = sftp.ls(fielPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
return v;
}
public static Session getSession(String ip, String user, String psw, int port)throws Exception{
Session session = null;
JSch jsch = new JSch();
if (port <= 0) {
//连接服务器,采用默认端口
session = jsch.getSession(user, ip);
} else {
session = jsch.getSession(user, ip, port);
}
//如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("sftp session is null");
}
session.setPassword(psw);//设置密码
//设置登陆超时时间
session.connect(30000);//30s
return session;
}
}
以上就是JSch教程使用sftp协议实现服务器文件上传下载操作的详细内容,更多关于JSch sftp协议服务器文件上传下载的资料请关注编程网其它相关文章!
--结束END--
本文标题: JSch教程使用sftp协议实现服务器文件载操作
本文链接: https://www.lsjlt.com/news/141643.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-18
2024-04-17
2024-04-11
2024-04-08
2024-04-08
2024-04-03
2024-03-15
2024-03-15
2024-03-11
2024-03-08
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0