首页

通过JCraft的jsch包基于SSH2协议实现连接linux服务器提供终端操作的java工具实现类SftpUtil

标签:JCraft,SSH2,linux,终端连接,示例,终端访问,远程服务,jsch-0.1.42.jar,同步,util,ftp,文件传输,sftp,安全,加密,secure     发布时间:2016-11-30   

通过JCraft的JSch的依赖包(jsch-0.1.42.jar下载)实现SSH2协议远程通信,如远程连接到linux服务器进行文件上传下载或数据同步,具体代码示例如下所示

import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.util.Properties;@b@import java.util.Vector;@b@@b@import com.jcraft.jsch.Channel;@b@import com.jcraft.jsch.ChannelSftp;@b@import com.jcraft.jsch.JSch;@b@import com.jcraft.jsch.Session;@b@import com.jcraft.jsch.SftpException;@b@@b@public class SftpUtil {@b@    @b@    private Properties sftpInfo = new Properties();@b@    private ChannelSftp sftp = null;//sftp连接@b@    private Session sshSession=null;@b@    @b@    {@b@        try {@b@            sftpInfo.load(SftpUtil.class.getResourceAsStream("/sftpInfo.properties"));@b@        } catch (IOException e) {@b@            // TODO 自动生成的 catch 块@b@            e.printStackTrace();@b@        }@b@    }@b@@b@    private SftpUtil() {@b@    }@b@@b@    public static SftpUtil getInstance() {@b@        return new SftpUtil();@b@    }@b@@b@    /**@b@     * 连接sftp服务器@b@     * @return ChannelSftp sftp连接@b@     */@b@    public ChannelSftp connect() {@b@        String username=sftpInfo.getProperty("username");//用户名@b@        String host=sftpInfo.getProperty("host");//主机@b@        int port=Integer.parseInt(sftpInfo.getProperty("port"));//端口@b@        String password=sftpInfo.getProperty("password");//密码@b@        if(this.sftp==null){@b@            try {@b@                JSch jsch = new JSch();@b@                jsch.getSession(username, host, port);@b@                this.sshSession = jsch.getSession(username, host, port);//创建session@b@    @b@                //下面开始设置session参数@b@                this.sshSession.setPassword(password);@b@                Properties sshConfig = new Properties();@b@                sshConfig.put("StrictHostKeyChecking", "no");@b@                this.sshSession.setConfig(sshConfig);@b@                this.sshSession.setTimeout(60000);@b@                //session连接@b@                this.sshSession.connect();@b@                Channel channel = sshSession.openChannel("sftp");@b@                channel.connect();@b@                this.sftp = (ChannelSftp) channel;//转换为sftp连接@b@                System.out.println("连接成功。。。");@b@            } catch (Exception e) {@b@                System.out.println("连接异常。。。");@b@                e.printStackTrace();@b@            }@b@        }@b@        return sftp;@b@    }@b@@b@    /**@b@     * 上传文件@b@     * @param uploadFile 要上传的文件@b@     * @param filePath 要上传文件的路径@b@     * @return boolean 是否上传成功@b@     */@b@    public boolean upload(String uploadFile) {@b@        try {@b@            this.sftp.cd(sftpInfo.getProperty("uploadPath"));@b@            File file = new File(uploadFile);@b@            this.sftp.put(new FileInputStream(file), file.getName());@b@            return true;@b@        } catch (SftpException e) {@b@            e.printStackTrace();@b@            return false;@b@        } catch (FileNotFoundException e) {@b@            e.printStackTrace();@b@            return false;@b@        }@b@    }@b@@b@    /**@b@     * 下载文件@b@     * @param downloadFile 下载的文件@b@     * @param saveFile 存在本地的路径@b@     * @return boolean 是否下载成功@b@     */@b@    public boolean download(String downloadFile, String saveFile) {@b@        try {@b@            this.sftp.cd(sftpInfo.getProperty("downloadPath"));@b@            File file = new File(saveFile);@b@            this.sftp.get(downloadFile, new FileOutputStream(file));@b@            return true;@b@        } catch (SftpException e) {@b@            e.printStackTrace();@b@            return false;@b@        } catch (FileNotFoundException e) {@b@            e.printStackTrace();@b@            return false;@b@        }@b@    }@b@    @b@    /**@b@     * 下载文件@b@     * @param downloadFile 下载的文件@b@     * @return InputStream 返回一个输入流@b@     */@b@    public InputStream download(String downloadFile) {@b@        try {@b@            this.sftp.cd(sftpInfo.getProperty("downloadPath"));@b@            return sftp.get(downloadFile);@b@        } catch (SftpException e) {@b@            e.printStackTrace();@b@            return null;@b@        } @b@    }@b@@b@    /**@b@     * 删除文件@b@     * @param deleteFile 要删除的文件@b@     * @return boolean 是否删除成功@b@     */@b@    public boolean delete(String deleteFile) {@b@        try {@b@            this.sftp.cd(sftpInfo.getProperty("downloadPath"));@b@            this.sftp.rm(deleteFile);@b@            return true;@b@        } catch (SftpException e) {@b@            e.printStackTrace();@b@            return false;@b@        }@b@    }@b@@b@    /**@b@     * 列出目录下的全部文件@b@     * @param filePath 要遍历文件的路径@b@     * @return Vector 一个保存有文件的Vector@b@     */@b@    @SuppressWarnings("unchecked")@b@    public Vector listFiles(String filePath)@b@            throws SftpException {@b@        return sftp.ls(sftpInfo.getProperty(filePath));@b@    }@b@    @b@    /**@b@     * 关闭连接@b@     * */@b@    public void disconnect(){@b@        if(this.sftp != null){@b@            if(this.sftp.isConnected()){@b@                this.sftp.disconnect();@b@                System.out.println("sftp连接关闭");@b@            }else if(this.sftp.isClosed()){@b@                System.out.println("连接已经被关闭");@b@            }@b@        }@b@        if(this.sshSession!=null){@b@            if(this.sshSession.isConnected()){@b@                this.sshSession.disconnect();@b@                System.out.println("session连接关闭");@b@            }@b@        }@b@    }@b@    @b@    public static void main(String args[]){@b@        SftpUtil ftpUtil = SftpUtil.getInstance();@b@        try {@b@            if (ftpUtil.connect().isConnected()) {@b@                // 测试上传@b@                ftpUtil.upload("C:/WS/NJ/test/ENV.txt");@b@                // 测试下载@b@                ftpUtil.download("test.txt", "C:/WS/NJ/test/download.txt");@b@            }@b@        } catch (Exception e) {@b@            e.printStackTrace();@b@        } finally {@b@            ftpUtil.disconnect();@b@        }@b@  }@b@}

sftpInfo.properties的配置内容如下

host=www.xwood.net@b@username=root@b@password=123456@b@port=22@b@uploadPath=/usr/local/test/upload@b@downloadPath=/usr/local/test/download

��