Search in sources :

Example 36 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, 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 37 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp 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 38 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project linuxtools by eclipse.

the class SSHFileStore method fetchInfo.

@Override
public IFileInfo fetchInfo(int options, IProgressMonitor monitor) throws CoreException {
    if (monitor == null)
        monitor = new NullProgressMonitor();
    try {
        monitor.beginTask(Messages.SSHFileStore_attrMonitor, 100);
        ChannelSftp channel = proxy.getChannelSftp();
        monitor.worked(25);
        SftpATTRS attrs = channel.stat(uri.getPath());
        monitor.worked(100);
        monitor.done();
        return createFileInfo(attrs);
    } catch (SftpException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHFileStore_attrFailed + e.getMessage()));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ChannelSftp(com.jcraft.jsch.ChannelSftp) CoreException(org.eclipse.core.runtime.CoreException) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 39 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project linuxtools by eclipse.

the class SSHFileStore method childStores.

@Override
public IFileStore[] childStores(int options, IProgressMonitor monitor) throws CoreException {
    if (monitor == null)
        monitor = new NullProgressMonitor();
    try {
        monitor.beginTask(Messages.SSHFileStore_childStoresMonitor, 100);
        ChannelSftp channel = proxy.getChannelSftp();
        monitor.worked(25);
        Vector<?> v = channel.ls(uri.getPath());
        monitor.worked(50);
        LinkedList<IFileStore> childs = new LinkedList<>();
        boolean isDir = false;
        for (int i = 0; i < v.size(); i++) {
            ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) v.get(i);
            if (// $NON-NLS-1$ //$NON-NLS-2$
            !entry.getFilename().equals(".") && !entry.getFilename().equals(".."))
                childs.add(createFileStore(path.append(entry.getFilename()).toString()));
            else
                isDir = true;
        }
        if (!isDir)
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.SSHFileStore_childStoresFailedDirectory, getName())));
        monitor.worked(100);
        monitor.done();
        return childs.toArray(new IFileStore[0]);
    } catch (SftpException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHFileStore_childStoresFailed + e.getMessage()));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SftpException(com.jcraft.jsch.SftpException) LinkedList(java.util.LinkedList) ChannelSftp(com.jcraft.jsch.ChannelSftp) CoreException(org.eclipse.core.runtime.CoreException) IFileStore(org.eclipse.core.filesystem.IFileStore)

Example 40 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project linuxtools by eclipse.

the class SSHFileStore method childInfos.

@Override
public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        monitor.beginTask(Messages.SSHFileStore_childInfoMonitor, 100);
        ChannelSftp channel = proxy.getChannelSftp();
        monitor.worked(25);
        Vector<?> v = channel.ls(uri.getPath());
        monitor.worked(50);
        LinkedList<IFileInfo> childs = new LinkedList<>();
        boolean isDir = false;
        for (int i = 0; i < v.size(); i++) {
            ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) v.get(i);
            if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                childs.add(createFileInfo(entry.getAttrs()));
            } else {
                isDir = true;
            }
        }
        if (!isDir) {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, MessageFormat.format(Messages.SSHFileStore_childInfoFailedDirectory, getName())));
        }
        monitor.worked(100);
        monitor.done();
        return childs.toArray(new IFileInfo[0]);
    } catch (SftpException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.SSHFileStore_childInfoFailed + e.getMessage()));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SftpException(com.jcraft.jsch.SftpException) LinkedList(java.util.LinkedList) ChannelSftp(com.jcraft.jsch.ChannelSftp) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)42 SftpException (com.jcraft.jsch.SftpException)24 IOException (java.io.IOException)15 JSchException (com.jcraft.jsch.JSchException)12 Session (com.jcraft.jsch.Session)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 File (java.io.File)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)6 JSch (com.jcraft.jsch.JSch)6 Path (org.apache.hadoop.fs.Path)6 InputStream (java.io.InputStream)5 SftpATTRS (com.jcraft.jsch.SftpATTRS)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 LinkedList (java.util.LinkedList)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 OutputStream (java.io.OutputStream)2