jsch 判断文件是否存在_java sftp判断目录是否存在

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();

}

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值