Search in sources :

Example 16 with SftpException

use of com.jcraft.jsch.SftpException in project ats-framework by Axway.

the class SftpClient method performDownloadFile.

@Override
protected void performDownloadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    FileOutputStream fos = null;
    try {
        // change to correct remote directory, but save the one that the
        // client was in before that
        String currentDir = this.channel.pwd();
        this.channel.cd(remoteDir);
        // download the file
        File file = new File(localFile);
        fos = new FileOutputStream(localFile);
        if (isDebugMode() && debugProgressMonitor != null) {
            debugProgressMonitor.setTransferMetadata(localFile, remoteFile, file.length());
            this.channel.get(remoteFile, fos, debugProgressMonitor);
        } else {
            this.channel.get(remoteFile, fos);
        }
        // go back to the location that the client was before the upload
        // took place
        this.channel.cd(currentDir);
    } catch (SftpException e) {
        log.error("Unable to download " + localFile, e);
        throw new FileTransferException(e);
    } catch (FileNotFoundException e) {
        log.error("Unable to create " + localFile, e);
        throw new FileTransferException(e);
    } finally {
        // close the file output stream
        IoUtils.closeStream(fos, "Unable to close the file stream after successful download!");
    }
    if (remoteDir != null && !remoteDir.endsWith("/")) {
        remoteDir += "/";
    }
    log.info("Successfully downloaded '" + localFile + "' from '" + remoteDir + remoteFile + "', host " + this.hostname);
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FileOutputStream(java.io.FileOutputStream) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 17 with SftpException

use of com.jcraft.jsch.SftpException 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 18 with SftpException

use of com.jcraft.jsch.SftpException 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 19 with SftpException

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

the class SFTPFileSystem method getFileStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
@SuppressWarnings("unchecked")
private FileStatus getFileStatus(ChannelSftp client, Path file) throws IOException {
    FileStatus fileStat = null;
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    Path parentPath = absolute.getParent();
    if (parentPath == null) {
        // root directory
        // Length of root directory on server not known
        long length = -1;
        boolean isDir = true;
        int blockReplication = 1;
        // Block Size not known.
        long blockSize = DEFAULT_BLOCK_SIZE;
        // Modification time of root directory not known.
        long modTime = -1;
        Path root = new Path("/");
        return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory()));
    }
    String pathName = parentPath.toUri().getPath();
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(pathName);
    } catch (SftpException e) {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    if (sftpFiles != null) {
        for (LsEntry sftpFile : sftpFiles) {
            if (sftpFile.getFilename().equals(file.getName())) {
                // file found in directory
                fileStat = getFileStatus(client, sftpFile, parentPath);
                break;
            }
        }
        if (fileStat == null) {
            throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
        }
    } else {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    return fileStat;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 20 with SftpException

use of com.jcraft.jsch.SftpException 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)

Aggregations

SftpException (com.jcraft.jsch.SftpException)45 IOException (java.io.IOException)18 ChannelSftp (com.jcraft.jsch.ChannelSftp)17 Path (org.apache.hadoop.fs.Path)14 File (java.io.File)13 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)8 JSchException (com.jcraft.jsch.JSchException)8 FileStatus (org.apache.hadoop.fs.FileStatus)8 Session (com.jcraft.jsch.Session)7 FileNotFoundException (java.io.FileNotFoundException)7 SftpATTRS (com.jcraft.jsch.SftpATTRS)6 Channel (com.jcraft.jsch.Channel)5 OutputStream (java.io.OutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3