SFTP环境搭建及客户代码调用公共方法封装

一、背景

在开发应用软件的过程中,广泛使用FTP在各子系统间传送文本数据。但FTP存在安全问题,开放到外网存在安全漏洞,容易被攻击。替换方案是使用SFTP,SFTP提供更高的安全性,当然传输的效率也会有影响。实际使用中使用FTP还是SFTP需要根据系统使用的环境综合评估决定。
本文档在继上一篇描述了FTP环境构建的基础上,进一步描述SFTP的环境构建和客户端调用公共方法的封装。

二、SFTP Server环境搭建

SFTP服务端工具很多。作为研究用,选择 Core FTP Server,工具小巧使用简单(选择的是免费min版,只同时支持一个客户端连接)。
下载链接:http://www.coreftp.com/server/index.html

三、客户端调用公共基础类封装

3.1 添加jsch-0.1.51.jar

3.2 公共代码封装

/**
 * 
 * SFTP访问公共类
 *
 * @author elon
 * @version 1.0, 2015年10月31日
 */
public class SFTPUtility { // SFTP连接参数 private String sftpIP; private int sftpPort; private String userName; private String password; // 超时时间 private int timeout; // SFTP会话 private Session sftpSession; // SFTP通道 private ChannelSftp sftpChannel; public SFTPUtility(String sftpIP, int sftpPort, String userName, String password, int timeout) { this.sftpIP = sftpIP; this.sftpPort = sftpPort; this.userName = userName; this.password = password; this.timeout = timeout; sftpSession = null; sftpChannel = null; } /** * * 链接SFTP服务器。 * * @throws JSchException * @throws SftpException */ public void connect() throws JSchException, SftpException { JSch jsch = new JSch(); // 创建会话连接 sftpSession = jsch.getSession(userName, sftpIP, sftpPort); // 设置密码 sftpSession.setPassword(password); sftpSession.setConfig("StrictHostKeyChecking", "no"); sftpSession.setTimeout(timeout); // 创建会话 sftpSession.connect(); // 打开SFTP通道 sftpChannel = (ChannelSftp)sftpSession.openChannel("sftp"); sftpChannel.connect(); sftpChannel.setFilenameEncoding("UTF-8"); } /** * 上传文件到SFTP服务器 * @param localFilePath 待上传的本地文件完整路径 * @param sftpFileName 上传后保存到SFTP服务器的名称(一般和本地文件名一致) * @throws SftpException * @throws IOException IO异常 */ public void upLoadFile(String localFilePath, String sftpFileName) throws SftpException { sftpChannel.put(localFilePath, sftpFileName); } /** * 从SFTP服务器下载文件到本地。 * @param sftpFileName 所下载文件在FTP服务器上的名称 * @param localFilePath 下载后文件保存的完整路径(文件名一般和SFTP上保存的文件一致) * @throws SftpException * @throws IOException IO异常 */ public void downLoadFile(String sftpFileName, String localFilePath) throws SftpException { sftpChannel.get(sftpFileName, localFilePath); } /** * 关闭SFTP连接 * @throws IOException */ public void disconnet() throws IOException { if (sftpChannel != null) { sftpChannel.disconnect(); } if (sftpSession != null) { sftpSession.disconnect(); } } }

 

四、测试函数

public class TestMain
{
    public static void main(String[] args) throws JSchException, SftpException, IOException { SFTPUtility sftp = new SFTPUtility("10.70.69.69", 22, "sftp", "sftp", 100000); sftp.connect(); sftp.upLoadFile("D:/TEMP/V1R2C10分配 - 副本.xlsx", "V1R2C10分配 - 副本.xlsx"); sftp.downLoadFile("V1R2C10分配 - 副本.xlsx", "D:/TEMP/V1R2C10分配 - 副本 new.xlsx"); sftp.disconnet(); } }

转载于:https://www.cnblogs.com/telwanggs/p/7448534.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值