java jsch下载文件,JSch使用sftp协议实现服务器文件上传下载操作

Jsch是什么?

JSch 是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。你可以将它的功能集成到你自己的 程序中。同时该项目也提供一个J2ME版本用来在手机上直连SSHD服务器

Jsch功能很强大,博主这里主要用来做文件操作

怎么使用?

添加jar依赖

com.jcraft

jsch

0.1.53

我把我的SftpUtil贴下面了,注释还算清楚

/**

* Content :sftp协议文件上传下载

* Created by kl on 2016/5/6.

*/

public class SftpUtil {

/**

* 上传文件到指定服务器

* @param ip 连接ip

* @param user 用户名

* @param psw 密码

* @param port 端口 <=0 为默认端口

* @param fielPath 上传的服务器路径

* @param serverFileName 服务器保存的文件名

* @param instream 要上传的文件流

* @throws Exception

*/

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();

}

}

/**

* 从指定服务器下载文件到本地

* @param ip 连接ip

* @param user 用户名

* @param psw 密码

* @param port 端口 <=0 为默认端口

* @param fielPath 服务器文件路径

* @param serverFileName 要下载的文件名

* @param outputStream 输出到本地的文件流

* @throws Exception

*/

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();

}

}

/**

* 删除服务器指定文件

* @param ip 连接ip

* @param user 用户名

* @param psw 密码

* @param port 端口 <=0 为默认端口

* @param fielPath 服务器文件路径

* @param serverFileName 要删除的文件名

* @throws Exception

*/

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();

}

}

/**

* 查看指定目录所有文件

* @param ip

* @param user

* @param psw

* @param port

* @param fielPath

* @throws Exception

*/

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;

}

/**

* 连接服务器

* @param ip 服务器地址

* @param user 用户名

* @param psw 密码

* @param port 连接端口

* @return

* @throws Exception

*/

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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值