javasftp判断⽬录是否存在java sftp判断⽬录是否存在
public boolean isExistDir(String path,ChannelSftp sftp){
boolean  isExist=false;
try {
SftpATTRS sftpATTRS = sftp.lstat(path);
isExist = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isExist = false;
}
}
return isExist;
}
完整sftpUtil.class
package 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 = Logger(SftpUtils.class);
protected String host;
protected int port;
protected String username;
protected String password;
Session sshSession = null ;
/**
* @param host ip
* @param port 端⼝
* @param username 账号
* @param password 密码
*/
public SftpUtils(String host, int port, String username, String password) {
set(host, port, username, password);
}
public void set(String host, int port, 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();
sshSession = Session(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);
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");
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.setInputStream(null);
InputStream in = InputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));            String buf = null;
while ((buf = adLine()) != null)
{
System.out.println(buf);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
channelExec = null;
}finally {
channelExec.disconnect();
}
}
/**
* linux上传⽂件
*/
public void upload(String directory, File file) {
ChannelSftp sftp = connectSftp();
try {
if (null != sftp) {
sftp.cd(directory);
logger.info("cd {}", directory);
FileInputStream stream = new FileInputStream(file);
try {
sftp.put(stream, Name());
} catch (Exception e) {
<("upload error", e);
} finally {
stream.close();
}
}
} catch (Exception e) {
<("upload:" + host, e);
} finally {
if (sftp != null) {
sftp.disconnect();
}
sshSession.disconnect();
}
}
/**
* linux下载⽂件
*/
public void get(String remotePath,String remoteFilename,String localFileName) {        ChannelSftp sftp = connectSftp();
File file=null;
OutputStream output=null;
try {
if (null != sftp) {
file = new File(localFileName);
if (ists()){
file.delete();
}
sftp.cd(remotePath);
output=new FileOutputStream(file);
try {
logger.info("Start download file:{},the arm file name:{}",remotePath+remoteFilename,localFileName);                    (remoteFilename, output);
logger.info("down {} suc", remotePath+remoteFilename);
} catch (Exception e) {
<("download error", e);
} finally {
output.close();
}
}
} catch (Exception e) {
<("down error :" , e);
} finally {
close(sftp);
}
}
protected void close(ChannelSftp sftp) {
if (sftp != null) {
session下载
sftp.disconnect();
}
if (sshSession != null) {
sshSession.disconnect();
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。