1. JAVA下载sftp文件
// 作者:付博
// 功能描述:下载sftp文件
package sftp;
import java.util.Properties;
import com.jcraft.jsch.*;
public class SftpDownload {
private static final String host = "192.168.131.131";
private static final int port = 22;
private static final String username = "fb";
private static final String passwd = "Fb123456";
private static final String localFilePath = "C:\\Users\\Administrator\\Desktop\\jiefangdai_callout\\sftp_file\\oracle.txt";
private static final String downloadFilePath = "/home/fb/test999/oracle.txt";
private static ChannelSftp channelsftp;
public static void getconn(String host, int port, String username, String passwd){
channelsftp = null;
JSch jsch = new JSch();
try {
Session session = jsch.getSession(username,host,port);
session.setPassword(passwd);
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
session.setConfig(properties);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
channelsftp = (ChannelSftp) channel;
}
catch (JSchException e){
e.printStackTrace();
}
}
public void disconnect() {
try {
channelsftp.getSession().disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
channelsftp.quit();
channelsftp.disconnect();
}
public void download(String downloadFilePath,String localFilePath){
try {
channelsftp.get(downloadFilePath, localFilePath);
}catch(Exception e){
e.printStackTrace();
System.out.println("download error.");
}
}
public static void main(String[] args) {
final SftpDownload SFTPinstance = new SftpDownload();
try{
SFTPinstance.getconn(host,port,username,passwd);
SFTPinstance.download(downloadFilePath,localFilePath);
}catch(Exception e){
e.printStackTrace();
}
SFTPinstance.disconnect();
}
}