import com.jcraft.jsch.Channel;
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 org.apache.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;
/**
* ssh服务器
*
* @author
* @create 2017-11-30 15:52
**/
public class SFTPUtil {
private static final Logger logger = Logger.getLogger(SFTPUtil.class);
/** Sftp */
ChannelSftp sftp = null;
/** 主机 */
private String host = "";
/** 端口 */
private int port = 0;
/** 用户名 */
private String username = "";
/** 密码 */
private String password = "";
public SFTPUtil( String host, int port, String username, String password) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}
public int testConnect(){
return connect ()?1:0;
}
/**
* 连接sftp服务器
*
* @throws Exception
*/
private boolean connect() {
JSch jsch = new JSch();
Session sshSession = null;
try {
sshSession = jsch.getSession(this.username, this.host, this.port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect(60000);
Channel channel = sshSession.openChannel("sftp");
channel.connect();
this.sftp = (ChannelSftp) channel;
return true;
} catch (JSchException e) {
e.printStackTrace ();
return false;
}
}
/**
* Disconnect with server
*
* @throws Exception
*/
private void disconnect() {
if(this.sftp != null){
if(this.sftp.isConnected()){
this.sftp.disconnect();
}else if(this.sftp.isClosed()){
}
}
}
/**
* 上传单个文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
*
* @throws Exception
*/
private boolean upload(String directory, InputStream uploadFile,String fileName) {
try {
this.sftp.cd(directory);
this.sftp.put(uploadFile, fileName);
return true;
} catch (SftpException e) {
e.printStackTrace ();
return false;
}
}
/**
* 上传文件
* @param host
* @param port
* @param username
* @param password
* @param directory
* @param uploadFile
* @param fileName
* @return
*/
public static boolean sftpConnect(String host, int port, String username, String password){
SFTPUtil sftpUtil = new SFTPUtil (host,port,username,password);
logger.info("SFTP开始连接 host:" + host + " port:" + port + "username:" + username + "password:" + password);
try {
// if(sftpUtil.connect ()){
// logger.info ("connect sftp success");
// boolean up = sftpUtil.upload (directory,uploadFile,fileName);
// if (up){
// logger.info("upload broadcast file success");
// return true;
// }else {
// logger.error("upload broadcast file fail");
// return false;
// }
//
// }else {
// logger.error("connect sftp fail");
// return false;
// }
}finally {
sftpUtil.disconnect();
logger.info ("sftp disconnect");
}
return false;
}
public static void main (String[] args) throws FileNotFoundException, SftpException {
SFTPUtil sftpUtil = new SFTPUtil ("",,"","");
if(sftpUtil.connect ()){
Vector vv = sftpUtil.sftp.ls("/config");
for(Object object : vv){
ChannelSftp.LsEntry entry=(ChannelSftp.LsEntry)object;
if(entry.getFilename().equals("db.properties")){
String fromFile = "/config" + entry.getFilename() ;
//默认在本项目文件夹下
String toFile = entry.getFilename();
sftpUtil.sftp.get(fromFile, toFile);
}
}
}else{
System.out.println(-1);
}
}
}
Java linux文件操作 ssh连接
最新推荐文章于 2022-12-10 15:24:40 发布