歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java使用SSH從遠程服務器下載文件

Java使用SSH從遠程服務器下載文件

日期:2017/3/1 9:44:22   编辑:Linux編程

前言

Telnet、FTP、POP3在網絡傳輸的過程中都是采用明文,容易被監聽或者遭到到man-in-the-middle的攻擊方式攻擊。而SSH為遠程登陸會話和其他的網絡服務提供安全協議,通過加密數據防止傳輸過程中信息洩漏。
C/C++可以調用OpenSSH來實現SSH,Java也有相應的開源類庫JSch,它是SSH2的一個純Java實現。

CentOS 下SSH無密碼登錄的配置 http://www.linuxidc.com/Linux/2012-05/61346.htm

Linux下實現SSH無密碼驗證登陸 http://www.linuxidc.com/Linux/2014-01/95917.htm

Ubuntu和CentOS如何配置SSH使得無密碼登陸 http://www.linuxidc.com/Linux/2014-01/94794.htm

源代碼

類SftpDownloader的源代碼:

package cn.aofeng;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import cn.aofeng.dto.SftpConnectInfo;
import cn.aofeng.util.ConsoleProgressBar;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.SftpProgressMonitor;

/**
* 使用SFTP從遠程主機下載文件。
*
* @author 傲風 <[email protected]>
*/
public class SftpDownloader {

private final static Logger logger = Logger.getLogger(SftpDownloader.class);

/**
*
使用SFTP從遠程主機下載文件
*
* @param connectInfo SFTP連接信息
* @param localPath 本地文件路徑
* @param remotePath 遠程文件路徑
*/
public void download(SftpConnectInfo connectInfo, String localPath, final String remotePath) {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channel = null;
OutputStream outs = null;
try {
session = jsch.getSession(connectInfo.getUsername(), connectInfo.getHost(), connectInfo.getPort());
session.setPassword(connectInfo.getPassword());
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
session.setConfig(props);
session.connect(5000); // 毫秒
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect(5000);
outs = new BufferedOutputStream(
new FileOutputStream(
new File(localPath)));
channel.get(remotePath, outs, new SftpProgressMonitor() {

ConsoleProgressBar progress = null;

private long current = 0;

@Override
public void init(int op, String src, String dest, long max) {
progress = new ConsoleProgressBar(0, max, 50);
}

@Override
public void end() {
if (logger.isInfoEnabled()) {
logger.debug( String.format("download file:%s complete", remotePath) );
}
}

@Override
public boolean count(long count) {
current += count;
progress.show(current);

return true;
}
});
} catch (JSchException e) {
logger.error( String.format("connect remote host[%s:%d] occurs error", connectInfo.getHost(), connectInfo.getPort()), e);
} catch (SftpException e) {
logger.error( String.format("get remote file:%s occurs error", remotePath), e);
} catch (FileNotFoundException e) {
logger.error( String.format("can not find local file:%s", localPath), e);
} finally {
IOUtils.closeQuietly(outs);
if (null != channel) {
channel.disconnect();
}
if (null != session) {
session.disconnect();
}
}
}
}

注:進度條ConsoleProgressBar的源代碼查看文章Java實現在控制終端顯示的字符進度條 http://www.linuxidc.com/Linux/2014-05/101839.htm

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-05/101840p2.htm

Copyright © Linux教程網 All Rights Reserved