Search in sources :

Example 56 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project voltdb by VoltDB.

the class ExportOnServerVerifier method checkForMoreExportFiles.

private void checkForMoreExportFiles() throws Exception {
    Comparator<String> comparator = new Comparator<String>() {

        @Override
        public int compare(String f1, String f2) {
            f1 = f1.substring(f1.lastIndexOf('/') + 1);
            f2 = f2.substring(f2.lastIndexOf('/') + 1);
            Matcher m1 = EXPORT_FILENAME_REGEXP.matcher(f1);
            Matcher m2 = EXPORT_FILENAME_REGEXP.matcher(f2);
            if (m1.matches() && !m2.matches())
                return -1;
            else if (m2.matches() && !m1.matches())
                return 1;
            long first_ts = Long.parseLong(m1.group(2));
            long second_ts = Long.parseLong(m2.group(2));
            if (first_ts != second_ts) {
                return (int) (first_ts - second_ts);
            } else {
                long first_txnid = Long.parseLong(m1.group(1));
                long second_txnid = Long.parseLong(m2.group(1));
                if (first_txnid < second_txnid) {
                    return -1;
                } else if (first_txnid > second_txnid) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    };
    checkForMoreFilesRemote(comparator);
    for (Pair<ChannelSftp, String> p : m_exportFiles) {
        System.out.println("" + p.getFirst().getSession().getHost() + " : " + p.getSecond());
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Matcher(java.util.regex.Matcher) Comparator(java.util.Comparator)

Example 57 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project voltdb by VoltDB.

the class ExportOnServerVerifier method openNextExportFile.

Pair<BufferedReader, Runnable> openNextExportFile() throws Exception {
    if (m_exportFiles.isEmpty()) {
        checkForMoreExportFiles();
    }
    Pair<ChannelSftp, String> remotePair = m_exportFiles.poll();
    if (remotePair == null)
        return null;
    final ChannelSftp channel = remotePair.getFirst();
    final String path = remotePair.getSecond();
    System.out.println("INFO export: Opening export file: " + channel.getSession().getHost() + "@" + path);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(channel.get(path)), 4096 * 32);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                reader.close();
                channel.rm(path);
            } catch (Exception e) {
                Throwables.propagate(e);
            }
        }
    };
    return Pair.of(reader, r);
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 58 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp 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 59 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 60 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)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)99 SftpException (com.jcraft.jsch.SftpException)61 IOException (java.io.IOException)36 JSchException (com.jcraft.jsch.JSchException)28 Session (com.jcraft.jsch.Session)25 JSch (com.jcraft.jsch.JSch)20 Channel (com.jcraft.jsch.Channel)17 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)17 File (java.io.File)16 Test (org.junit.Test)14 InputStream (java.io.InputStream)12 SftpATTRS (com.jcraft.jsch.SftpATTRS)11 FileNotFoundException (java.io.FileNotFoundException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 FileInputStream (java.io.FileInputStream)7 OutputStream (java.io.OutputStream)7 Path (org.apache.hadoop.fs.Path)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7