Search in sources :

Example 46 with SftpException

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

the class SFTPFileSystem method open.

@Override
public FSDataInputStream open(Path f, int bufferSize) throws IOException {
    ChannelSftp channel = connect();
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    FileStatus fileStat = getFileStatus(channel, absolute);
    if (fileStat.isDirectory()) {
        disconnect(channel);
        throw new IOException(String.format(E_PATH_DIR, f));
    }
    InputStream is;
    try {
        // the path could be a symbolic link, so get the real path
        absolute = new Path("/", channel.realpath(absolute.toUri().getPath()));
        is = channel.get(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataInputStream fis = new FSDataInputStream(new SFTPInputStream(is, channel, statistics));
    return fis;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) FileStatus(org.apache.hadoop.fs.FileStatus) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException)

Example 47 with SftpException

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

the class SFtpClientUtils method put.

/**
	 * 本地檔案放到遠端SFTP上
	 * 	
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param localFile
	 * @param remoteFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void put(String user, String password, String addr, int port, List<String> localFile, List<String> remoteFile) 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 < localFile.size(); i++) {
            String rf = remoteFile.get(i);
            String lf = localFile.get(i);
            logger.info("put local file: " + lf + " write to " + addr + " :" + rf);
            sftpChannel.put(lf, rf);
            logger.info("success write to " + addr + " :" + rf);
        }
    } 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 48 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, String remoteFile, String localFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    logger.info("get remote file: " + remoteFile + " write to:" + localFile);
    try {
        sftpChannel.get(remoteFile, localFile);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    File f = new File(localFile);
    if (!f.exists()) {
        f = null;
        logger.error("get remote file:" + remoteFile + " fail!");
        throw new Exception("get remote file:" + remoteFile + " fail!");
    }
    f = null;
    logger.info("success write:" + localFile);
}
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 49 with SftpException

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

the class SFtpClientUtils method getRemoteFileList.

@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Vector<LsEntry> lsVec = null;
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        //sftpChannel.lpwd()
        lsVec = (Vector<LsEntry>) sftpChannel.ls(cwd);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
    return lsVec;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Example 50 with SftpException

use of com.jcraft.jsch.SftpException in project Lucee by lucee.

the class SFTPClientImpl method listFiles.

@Override
public FTPFile[] listFiles(String pathname) throws IOException {
    pathname = cleanPath(pathname);
    List<FTPFile> files = new ArrayList<FTPFile>();
    try {
        Vector list = channelSftp.ls(pathname);
        Iterator<ChannelSftp.LsEntry> it = list.iterator();
        ChannelSftp.LsEntry entry;
        SftpATTRS attrs;
        FTPFile file;
        String fileName;
        while (it.hasNext()) {
            entry = it.next();
            attrs = entry.getAttrs();
            fileName = entry.getFilename();
            if (fileName.equals(".") || fileName.equals(".."))
                continue;
            file = new FTPFile();
            files.add(file);
            // is dir
            file.setType(attrs.isDir() ? FTPFile.DIRECTORY_TYPE : FTPFile.FILE_TYPE);
            file.setTimestamp(Caster.toCalendar(attrs.getMTime() * 1000L, null, Locale.ENGLISH));
            file.setSize(attrs.isDir() ? 0 : attrs.getSize());
            FTPConstant.setPermission(file, attrs.getPermissions());
            file.setName(fileName);
        }
        handleSucess();
    } catch (SftpException e) {
        handleFail(e, stopOnError);
    }
    return files.toArray(new FTPFile[files.size()]);
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) FTPFile(org.apache.commons.net.ftp.FTPFile) Vector(java.util.Vector)

Aggregations

SftpException (com.jcraft.jsch.SftpException)54 ChannelSftp (com.jcraft.jsch.ChannelSftp)26 IOException (java.io.IOException)18 Path (org.apache.hadoop.fs.Path)14 File (java.io.File)13 SftpATTRS (com.jcraft.jsch.SftpATTRS)9 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 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 Session (com.jcraft.jsch.Session)7 FileNotFoundException (java.io.FileNotFoundException)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7 Channel (com.jcraft.jsch.Channel)5 OutputStream (java.io.OutputStream)5 ArrayList (java.util.ArrayList)5 Vector (java.util.Vector)5