SFtp ssh2.0 version 上传、下载文件

SFtp ssh2.0 version 上传、下载文件


  public void upload(String filepath, String filename) throws Exception {
    logger.info("证件图像文件SFTP上传 ... ,\nfile = {}{}", filepath, filename);
    InputStream in = null;
    ChannelSftp sftp = null;
    Session session = null;

    try {

      JSch jsch=new JSch();
      session=jsch.getSession(UnionPayConfig.SFTP_USER, UnionPayConfig.SFTP_IP, UnionPayConfig.SFTP_PORT);
      session.setPassword(UnionPayConfig.SFTP_PWD);

      Properties sshConfig = new Properties();
      sshConfig.put("StrictHostKeyChecking", "no");
      session.setConfig(sshConfig);

      session.connect();

      sftp = (ChannelSftp)session.openChannel("sftp");
      sftp.connect();

      /*Vector<?> vector = sftp.ls(UnionPayConfig.SFTP_DIR);
      for (Object item:vector) {
        ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) item;
        logger.info(entry.getFilename());
        if( entry.getFilename().equals("10000000016690_QYZH00000000245_corp.zip") ) {
          sftp.get(UnionPayConfig.SFTP_DIR+"10000000016690_QYZH00000000245_corp.zip", new FileOutputStream("d:/10000000016690_QYZH00000000245_corp.zip"));
        }
      }*/

      byte[] bytes = new byte[1024];
      in = new FileInputStream(filepath+filename);
      OutputStream out = sftp.put(UnionPayConfig.SFTP_DIR + filename);

      while (in.read(bytes)  != -1) {
        out.write(bytes);
      }
      out.close();
      in.close();

      logger.info("证件图像文件SFTP上传 成功, filename = " + filename);

    } catch (Exception e) {
      logger.error("证件图像文件SFTP上传 失败", e);
      throw e;
    } finally {
      if (sftp != null) {
        if (sftp.isConnected()) {
          sftp.disconnect();
        }
      }
      if (session != null) {
        if (session.isConnected()) {
          session.disconnect();
        }
      }
      if (in != null) {
        in.close();
      }
    }

  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Windows系统上,你可以使用libssh和libcurl等第三方库来实现通过sftp和ssh上传本地文件。以下是一个简单的示例代码: ```cpp #include <windows.h> #include <curl/curl.h> #include <libssh/libssh.h> int main(int argc, char *argv[]) { // 初始化curl curl_global_init(CURL_GLOBAL_ALL); // 初始化ssh ssh_session ssh = ssh_new(); ssh_options_set(ssh, SSH_OPTIONS_HOST, "hostname"); ssh_options_set(ssh, SSH_OPTIONS_PORT, 22); ssh_options_set(ssh, SSH_OPTIONS_USER, "username"); ssh_connect(ssh); // 创建sftp会话 sftp_session sftp = sftp_new(ssh); sftp_init(sftp); // 打开本地文件 FILE* local_file = fopen("local_file", "rb"); if (local_file == NULL) { printf("Failed to open local file!\n"); return -1; } // 创建远程文件 sftp_file remote_file = sftp_open(sftp, "remote_file", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR); if (remote_file == NULL) { printf("Failed to create remote file!\n"); return -1; } // 从本地文件读取数据并写入远程文件 char buf[1024]; int len; while ((len = fread(buf, 1, sizeof(buf), local_file)) > 0) { sftp_write(remote_file, buf, len); } // 关闭文件sftp会话 fclose(local_file); sftp_close(remote_file); sftp_free(sftp); // 执行ssh命令 ssh_channel channel = ssh_channel_new(ssh); ssh_channel_open_session(channel); ssh_channel_request_exec(channel, "ssh_command"); ssh_channel_close(channel); ssh_channel_free(channel); // 关闭ssh会话 ssh_disconnect(ssh); ssh_free(ssh); // 清理curl curl_global_cleanup(); return 0; } ``` 其中,"hostname"、"username"、"local_file"、"remote_file"和"ssh_command"需要根据实际情况进行替换。 需要注意的是,在使用libssh和libcurl库时,需要在项目中添加相关的头文件和库文件,并且在编译时链接相关的库文件。在Windows系统中,还需要将libssh.dll和libcurl.dll文件复制到可执行文件所在的目录中。 如果以上步骤都没有解决问题,可以尝试重新编译相关的库文件,并确保在编译时包含了相关的模块和库文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值