JAVA访问SFTP

依赖

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

示例

/** 接收文件
	@ host:  SFTP 服务器IP地址: 192.168.xx.xxx
	@ port: S FTP 服务器端口: 22
	@ userName:  SFTP 服务器登录名称
	@ password: SFTP 服务器登录密码
	@ remotePath: SFTP 服务器文件目录: /home/ftpuser/
	@ remoteFileName: SFTP 服务器文件名称: xxxx.txt
	@ localFilePath: 本地文件目录: /data/
*/
public void download(String host, Integer port, String userName, String password, String remotePath, String remoteFileName, String localFilePath) {
    FileOutputStream fieloutput = null;
    Session sshSession = null;
    ChannelSftp sftp = null;
    try {
        JSch jsch = new JSch();
        sshSession = jsch.getSession(userName, host, port);
        sshSession.setPassword(password);

        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(sshConfig);
        sshSession.connect(30000);
        Channel channel = sshSession.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;

        File file = new File(localFilePath+“/”+fieloutput);
        fieloutput = new FileOutputStream(file);
        sftp.cd(remotePath);
        sftp.get(remotePath + remoteFileName, fieloutput);
    } catch (Exception e) {
        log.error("SFTP下载" + remotePath + remoteFileName + "文件异常:", e);
    } finally {
        if (null != fieloutput) {
            try {
                fieloutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (sftp != null) {
            if (sftp.isConnected()) {
                sftp.disconnect();
            }
        }
       if (sshSession != null) {
            if (sshSession.isConnected()) {
                sshSession.disconnect();
            }
        }
    }
}

/**  发送文件
	@ host:  SFTP 服务器IP地址
	@ port: S FTP 服务器端口
	@ userName:  SFTP 服务器登录名称
	@ password: SFTP 服务器登录密码
	@ remotePath: SFTP 服务器文件目录
	@ remoteFileName: SFTP 服务器文件名称
	@ localFilePath: 本地文件目录
*/
public static void upload(String host, int port, String userName, String password, String remoteFilePath, String remoteFileName, String localFilePath) throws EMPException {
   FileInputStream in = null;
   Session sshSession = null;
   ChannelSftp sftp = null;
   try {
      JSch jsch = new JSch();
      jsch.getSession(userName, host, port);
      sshSession = jsch.getSession(userName, host, port);
      sshSession.setPassword(password);

      Properties sshConfig = new Properties();
      sshConfig.put("StrictHostKeyChecking", "no");
      sshSession.setConfig(sshConfig);
      sshSession.connect();
      Channel channel = sshSession.openChannel("sftp");
      channel.connect();
      sftp = (ChannelSftp) channel;
      createDir(remoteFilePath, sftp);
      File file = new File(localFilePath+“/”+fieloutput);
      in = new FileInputStream(file);
      sftp.put(in, remoteFileName);
   } catch (Exception e) {
      throw new EMPException(e);
   } finally {
      if (in != null) {
         try {
            in.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
      if (sftp != null) {
         if (sftp.isConnected()) {
            sftp.disconnect();

         }
      }
      if (sshSession != null) {
         if (sshSession.isConnected()) {
            sshSession.disconnect();
         }
      }
   }
}

/**  删除文件
	@ host:  SFTP 服务器IP地址
	@ port: S FTP 服务器端口
	@ userName:  SFTP 服务器登录名称
	@ password: SFTP 服务器登录密码
	@ remotePath: SFTP 服务器文件目录
	@ remoteFileName: SFTP 服务器文件名称
*/
public void delete(String host, Integer port, String userName, String password, String remotePath, String remoteFileName) {
    FileOutputStream fieloutput = null;
    Session sshSession = null;
    ChannelSftp sftp = null;
    try {
        JSch jsch = new JSch();
        sshSession = jsch.getSession(userName, host, port);
        sshSession.setPassword(password);

        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(sshConfig);
        sshSession.connect(30000);
        Channel channel = sshSession.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;

        sftp.cd(remotePath);
        sftp.rm(remotePath + remoteFileName);
    } catch (Exception e) {
        log.error("SFTP删除" + remotePath + remoteFileName + "文件异常:", e);
    } finally {
        if (null != fieloutput) {
            try {
                fieloutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (sftp != null) {
            if (sftp.isConnected()) {
                sftp.disconnect();

            }
        }
        if (sshSession != null) {
            if (sshSession.isConnected()) {
                sshSession.disconnect();
            }
        }
    }
}

/**
   创建SFTP目录
*/
private static void createDir(String createpath, ChannelSftp sftp) {
      if (isDirExist(createpath, sftp)) {
         sftp.cd(createpath);
         return;
      }
      String pathArry[] = createpath.split("/");
      StringBuffer filePath = new StringBuffer("/");
      for (String path : pathArry) {
         if (path.equals("")) {
            continue;
         }
         filePath.append(path + "/");
         if (isDirExist(filePath.toString(), sftp)) {
            sftp.cd(filePath.toString());
         } else {
            // 建立目录
            sftp.mkdir(filePath.toString());
            // 进入并设置为当前目录
            sftp.cd(filePath.toString());
         }
      }
}
/**
  判断目录是否存在
*/
private static boolean isDirExist(String directory, ChannelSftp sftp) {
      SftpATTRS sftpATTRS = sftp.lstat(directory);
      return sftpATTRS.isDir();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值