package com.iot.crm.common.util;
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Properties;public class SftpUtils {
final Logger logger=LoggerFactory.getLogger(SftpUtils.class);
protected String host;
protectedintport;
protected String username;
protected String password;
Session sshSession= null;/**
* @param host ip
* @param port 端口
* @param username 账号
* @param password 密码*/public SftpUtils(String host,intport, String username, String password) {
set(host, port, username, password);
}
public void set(String host,intport, String username, String password) {
this.host=host;
this.port=port;
this.username=username;
this.password=password;
}
public Session getSession() {//Session sshSession = null;
try {
JSch jsch=new JSch();
jsch.getSession(this.username, this.host, this.port);
sshSession=jsch.getSession(this.username, this.host, this.port);
System.out.println("Session created.");
sshSession.setPassword(this.password);
Properties sshConfig=new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
} catch (Exception e) {
e.printStackTrace();
}
return sshSession;
}/**
/**
* 链接linux*/public ChannelSftp connectSftp() {
getSession();
ChannelSftp sftp= null;
try {
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel= sshSession.openChannel("sftp");
channel.connect();
sftp=(ChannelSftp) channel;
} catch (Exception e) {
e.printStackTrace();
sftp= null;
}
return sftp;
}
public void exec(Session session,String command) {
ChannelExec channelExec= null;
try {
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel= session.openChannel("exec");
channelExec=(ChannelExec) channel;
channelExec.setCommand(command);
channelExec.connect();
channelExec.setInputStream(null);
InputStreamin =channelExec.getInputStream();
BufferedReader reader= new BufferedReader(new InputStreamReader(in));
String buf= null;while ((buf = reader.readLine()) != null)
{
System.out.println(buf);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
channelExec= null;
}finally {
channelExec.disconnect();
}
}/**
* linux上传文件*/public void upload(String directory, Filefile) {
ChannelSftp sftp=connectSftp();
try {if (null !=sftp) {
sftp.cd(directory);
logger.info("cd {}", directory);
FileInputStream stream= new FileInputStream(file);
try {
sftp.put(stream,file.getName());
} catch (Exception e) {
logger.error("upload error", e);
} finally {
stream.close();
}
}
} catch (Exception e) {
logger.error("upload:" +host, e);
} finally {if (sftp != null) {
sftp.disconnect();
sftp.exit();
}
sshSession.disconnect();
}
}/**
* linux下载文件*/public void get(String remotePath,String remoteFilename,String localFileName) {
ChannelSftp sftp=connectSftp();
Filefile=null;
OutputStream output=null;
try {if (null !=sftp) {file =new File(localFileName);if (file.exists()){file.delete();
}file.createNewFile();
sftp.cd(remotePath);
output=new FileOutputStream(file);
try {
logger.info("Start download file:{},the arm file name:{}",remotePath+remoteFilename,localFileName);
sftp.get(remoteFilename, output);
logger.info("down {} suc", remotePath+remoteFilename);
} catch (Exception e) {
logger.error("download error", e);
} finally {
output.close();
}
}
} catch (Exception e) {
logger.error("down error :", e);
} finally {
close(sftp);
}
}
protected void close(ChannelSftp sftp) {if (sftp != null) {
sftp.disconnect();
sftp.exit();
}if (sshSession != null) {
sshSession.disconnect();
}
}}