软件编程
位置:首页>> 软件编程>> java编程>> JSch教程使用sftp协议实现服务器文件载操作

JSch教程使用sftp协议实现服务器文件载操作

作者:kl  发布时间:2023-10-29 17:43:33 

标签:JSch,sftp,服务器,文件操作

Jsch是什么?

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

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

怎么使用?

添加jar依赖

<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>

我把我的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;
   }
}

来源:http://www.kailing.pub/article/index/arcid/105.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com