Search in sources :

Example 11 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project bamboobsc by billchen198318.

the class SFtpClientUtils method get.

/**
	 * 抓遠端檔案然後存到本機 , 多筆
	 * 
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param remoteFile
	 * @param localFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void get(String user, String password, String addr, int port, List<String> remoteFile, List<String> localFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        for (int i = 0; i < remoteFile.size(); i++) {
            String rf = remoteFile.get(i);
            String lf = localFile.get(i);
            logger.info("get remote file: " + rf + " write to:" + lf);
            sftpChannel.get(rf, lf);
            File f = new File(lf);
            if (!f.exists()) {
                f = null;
                logger.error("get remote file:" + rf + " fail!");
                throw new Exception("get remote file:" + rf + " fail!");
            }
            f = null;
            logger.info("success write:" + lf);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) File(java.io.File) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 12 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project bamboobsc by billchen198318.

the class SFtpClientUtils method rm.

public static void rm(String user, String password, String addr, int port, List<String> fileName) throws JSchException, SftpException, Exception {
    if (fileName == null || fileName.size() < 1) {
        return;
    }
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        for (String f : fileName) {
            sftpChannel.rm(f);
            logger.warn("success remove file from " + addr + " :" + fileName);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 13 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPConnectionPool method connect.

public ChannelSftp connect(String host, int port, String user, String password, String keyFile) throws IOException {
    // get connection from pool
    ConnectionInfo info = new ConnectionInfo(host, port, user);
    ChannelSftp channel = getFromPool(info);
    if (channel != null) {
        if (channel.isConnected()) {
            return channel;
        } else {
            channel = null;
            synchronized (this) {
                --liveConnectionCount;
                con2infoMap.remove(channel);
            }
        }
    }
    // create a new connection and add to pool
    JSch jsch = new JSch();
    Session session = null;
    try {
        if (user == null || user.length() == 0) {
            user = System.getProperty("user.name");
        }
        if (password == null) {
            password = "";
        }
        if (keyFile != null && keyFile.length() > 0) {
            jsch.addIdentity(keyFile);
        }
        if (port <= 0) {
            session = jsch.getSession(user, host);
        } else {
            session = jsch.getSession(user, host, port);
        }
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        synchronized (this) {
            con2infoMap.put(channel, info);
            liveConnectionCount++;
        }
        return channel;
    } catch (JSchException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Session(com.jcraft.jsch.Session)

Example 14 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPFileSystem method create.

/**
   * A stream obtained via this call must be closed before using other APIs of
   * this class or else the invocation will block.
   */
@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    final ChannelSftp client = connect();
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    if (exists(client, f)) {
        if (overwrite) {
            delete(client, f, false);
        } else {
            disconnect(client);
            throw new IOException(String.format(E_FILE_EXIST, f));
        }
    }
    Path parent = absolute.getParent();
    if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
        parent = (parent == null) ? new Path("/") : parent;
        disconnect(client);
        throw new IOException(String.format(E_CREATE_DIR, parent));
    }
    OutputStream os;
    try {
        client.cd(parent.toUri().getPath());
        os = client.put(f.getName());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataOutputStream fos = new FSDataOutputStream(os, statistics) {

        @Override
        public void close() throws IOException {
            super.close();
            disconnect(client);
        }
    };
    return fos;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) OutputStream(java.io.OutputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream)

Example 15 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPFileSystem method connect.

/**
   * Connecting by using configuration parameters.
   *
   * @return An FTPClient instance
   * @throws IOException
   */
private ChannelSftp connect() throws IOException {
    Configuration conf = getConf();
    String host = conf.get(FS_SFTP_HOST, null);
    int port = conf.getInt(FS_SFTP_HOST_PORT, DEFAULT_SFTP_PORT);
    String user = conf.get(FS_SFTP_USER_PREFIX + host, null);
    String pwd = conf.get(FS_SFTP_PASSWORD_PREFIX + host + "." + user, null);
    String keyFile = conf.get(FS_SFTP_KEYFILE, null);
    ChannelSftp channel = connectionPool.connect(host, port, user, pwd, keyFile);
    return channel;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Configuration(org.apache.hadoop.conf.Configuration)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)31 SftpException (com.jcraft.jsch.SftpException)16 IOException (java.io.IOException)15 JSchException (com.jcraft.jsch.JSchException)12 Session (com.jcraft.jsch.Session)11 File (java.io.File)7 Channel (com.jcraft.jsch.Channel)6 JSch (com.jcraft.jsch.JSch)6 Path (org.apache.hadoop.fs.Path)6 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 SftpATTRS (com.jcraft.jsch.SftpATTRS)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 Path (java.nio.file.Path)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2 Configuration (org.apache.hadoop.conf.Configuration)2 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)2