Sftp上传hdfs文件

仅做记录用

package cn.com.xx.sftp;

import com.jcraft.jsch.*;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.*;
import java.util.Properties;


public class SFTPUtils
{
  public ChannelSftp sftp;
  public Session session;
//连接sftp
  public boolean connect(String path, String addr, int port, String username, String password) throws Exception {
    boolean result = false;
    try {
      JSch jSch = new JSch();
      session = jSch.getSession(username, addr, port);
      if (password != null) {
        session.setPassword(password);
      }
      Properties config = new Properties();
      config.put("StrictHostKeyChecking", "no");
      session.setConfig(config);
      session.connect();
      Channel channel = session.openChannel("sftp");
      sftp = (ChannelSftp) channel;
      result = true;
    } catch (JSchException e) {
      e.printStackTrace();
      return result;
    }


    if (!isDirExist(path)) {
      String[] tokens = StringUtils.splitPreserveAllTokens(path, "/");
      String newPath = "";
      for (int i = 1; i < tokens.length; i++) {
        try {
          newPath = newPath+"/"+tokens[i];
          sftp.cd(newPath);
        } catch (SftpException e) {
          sftp.mkdir(tokens[i]);
          sftp.cd(tokens[i]);
        }
        System.out.println(String.valueOf(i) + "======mkdir " + tokens[i]);
      }
      result = isDirExist(path);
    }
    return result;
  }

//判断路径是否存在
  public boolean isDirExist(String dir) {
    boolean isExist = false;
    try {
      SftpATTRS sftpATTRS = sftp.lstat(dir);
      isExist = true;
      return sftpATTRS.isDir();
    } catch (Exception e) {
      if (e.getMessage().toLowerCase().equals("no such file")) {
        isExist = false;
      }
    }
    return isExist;
  }

//hdfs文件上传到sftp
  public void upload(FileSystem fs, Path path, String newName,String outputPath) throws Exception {
    FSDataInputStream fSDataInputStream = fs.open(path, 8192);
    this.sftp.cd(outputPath);
    this.sftp.put((InputStream) fSDataInputStream, newName);
    fSDataInputStream.close();
  }

//本地文件上传到sftp
  public void uploadcheck(File file, String newName) throws Exception {
    FileInputStream input = new FileInputStream(file);
    this.sftp.put(input, file.getName());
    input.close();
  }

//关闭连接
  public void close() throws IOException {
    if (sftp != null) {
      if (sftp.isConnected()) {
        sftp.disconnect();
      }
    }
    if (session != null) {
      if (session.isConnected()) {
        session.disconnect();
      }
    }
  }




}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值