java sftp判断目录是否存在
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 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;
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();
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);
InputStream in = 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, 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, 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();
File file=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();
}
} }
java sftp判断目录是否存在的更多相关文章
- JAVA SFTP文件上传、下载及批量下载
JavaJsch 1.jsch官方API查看地址(附件为需要的jar) http://www.jcraft.com/jsch/ 2.jsch简介 JSch(Java Secure Channel)是 ...
- JAVA Sftp 上传下载
SftpUtils package xxx;import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com ...
- Eclipse JAVA项目的 目录结构 和 导入
说明:本文所有测试以java工程为例: 1. Eclipse下的java工程目录 eclipse的基本工程目录叫做workspace,每个运行时的eclipse实例只能对应一个workspace,也就 ...
- Java命名和目录接口——JNDI
JNDI即Java命名和目录接口(JavaNaming and Directory Interface),它属于J2EE规范范畴,是J2EE的核心技术之一,提供了一组接口.类和关于命名空间的概念.JD ...
- Java如何判断文件或者文件夹是否在?不存在如何创建?
Java如何判断文件或者文件夹是否在?不存在如何创建? 1. 首先明确一点的是:test.txt文件可以和test文件夹同时存在同一目录下:test文件不能和test文件夹同时存在同一目录下. 原 ...
- Java遍历一个目录下的所有文件
Java遍历一个目录下的所有文件 Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其他流类不同的是,流类关心的是文件的内容,而File类关心的是磁盘上文件的存 ...
- java代码实现目录结构
今天用java代码来实现.像我们电脑盘符那样的目录结构.在代码开始之前首先.介绍一下.用.java代码实现目录的思想. 第一步:完成基础的.大家想.我们是如何获取文件的.是不是用File类,直接就获取 ...
- Java SFTP 上传、下载等操作
Java SFTP 上传.下载等操作 实际开发中用到了 SFTP 用于交换批量数据文件,然后琢磨了下这方面的东西,基于 JSch 写了个工具类记录下,便于日后使用. JSch是 SSH2 的纯Java ...
- 深入Java虚拟机--判断对象存活状态
程序计数器,虚拟机栈和本地方法栈 首先我们先来看下垃圾回收中不会管理到的内存区域,在Java虚拟机的运行时数据区我们可以看到,程序计数器,虚拟机栈,本地方法栈这三个地方是比较特别的.这个三个部分的特点 ...
随机推荐
- [Python3 填坑] 009 深拷贝与浅拷贝
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...
- [ZJOI2009]取石子游戏
瞪了题解两三天,直接下转第二篇题解就康懂了 首先我们令 : \(L[i][j]\) 表示当前 \([i,j]\) 区间左侧放置 \(L[i,j]\) 数量的石子后先手必败 \(R[i][j]\) 表示 ...
- C#设计模式:中介者模式(Mediator Pattern)
一,什么是中介者模式(Mediator Pattern)? 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.比如:如果我们实现两个人的交互,难道我们要定义两个对象 ...
- 【学习总结】SQL学习总结
参考链接: 菜鸟教程: 一.认识sql 二.sql语法 三.sql高级教程 四.sql函数 一.认识SQL SQL是什么? SQL 是用于访问和处理数据库的标准的计算机语言. SQL,指结构化查询语言 ...
- IO流详解及测试代码
IO流 (1)IO用于在设备间进行数据传输的操作 (2)分类: A:流向 输入流 读取数据 输出流 写出数据 B:数据类型 字节流 字节输入流 ...
- win7安装scrapy
Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设 ...
- Python基础入门一文通 | Python2 与Python3及VSCode下载和安装、PyCharm激活与安装、Python在线IDE、Python视频教程
目录 1. 关键词 2. 推荐阅读 2.1. 视频教程 3. 本文按 4. 安装 4.1. 视频教程 4.2. 资源下载 4.3. 安装教程 1. 关键词 Python2 与Python3及VSCod ...
- lua视频教程三套高清新
目录 1. 下载地址 2. 某网校 Lua 经典教程 3. lua脚本语言零基础开发教程19课全 4. LUA完整视频+Cocos2d-x项目实战 1. 下载地址 https://www.cnblog ...
- Mysql查询结果导出Excel表
Mysql查询结果导出Excel表: 一句转换方式:$ mysql -uops -p'GCNgH000KP' dtbs -e 'select * from t_proxy__record;' --de ...
- MySQL concat函数里面单引号的使用
通过concat拼字符串的时候,如果语句里面需要使用单引号,可以使用两个单引号来代替一个引号