java sftp判断目录是否存在

  1. public boolean isExistDir(String path,ChannelSftp sftp){
  2. boolean isExist=false;
  3. try {
  4. SftpATTRS sftpATTRS = sftp.lstat(path);
  5. isExist = true;
  6. return sftpATTRS.isDir();
  7. } catch (Exception e) {
  8. if (e.getMessage().toLowerCase().equals("no such file")) {
  9. isExist = false;
  10. }
  11. }
  12. return isExist;
  13.  
  14. }

完整sftpUtil.class

  1. package com.iot.crm.common.util;
  2.  
  3. import com.jcraft.jsch.*;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6.  
  7. import java.io.*;
  8. import java.util.Properties;
  9.  
  10. public class SftpUtils {
  11.  
  12. final Logger logger = LoggerFactory.getLogger(SftpUtils.class);
  13.  
  14. protected String host;
  15. protected int port;
  16. protected String username;
  17. protected String password;
  18. Session sshSession = null ;
  19.  
  20. /**
  21. * @param host ip
  22. * @param port 端口
  23. * @param username 账号
  24. * @param password 密码
  25. */
  26. public SftpUtils(String host, int port, String username, String password) {
  27. set(host, port, username, password);
  28. }
  29.  
  30. public void set(String host, int port, String username, String password) {
  31. this.host = host;
  32. this.port = port;
  33. this.username = username;
  34. this.password = password;
  35. }
  36.  
  37. public Session getSession() {
  38. //Session sshSession = null;
  39. try {
  40. JSch jsch = new JSch();
  41. jsch.getSession(this.username, this.host, this.port);
  42. sshSession = jsch.getSession(this.username, this.host, this.port);
  43. System.out.println("Session created.");
  44. sshSession.setPassword(this.password);
  45. Properties sshConfig = new Properties();
  46. sshConfig.put("StrictHostKeyChecking", "no");
  47. sshSession.setConfig(sshConfig);
  48. sshSession.connect();
  49. System.out.println("Session connected.");
  50. System.out.println("Opening Channel.");
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. return sshSession;
  55. }
  56. /**
  57. /**
  58. * 链接linux
  59. */
  60. public ChannelSftp connectSftp() {
  61. getSession();
  62. ChannelSftp sftp = null;
  63. try {
  64. System.out.println("Session connected.");
  65. System.out.println("Opening Channel.");
  66. Channel channel = sshSession.openChannel("sftp");
  67. channel.connect();
  68. sftp = (ChannelSftp) channel;
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. sftp = null;
  72. }
  73. return sftp;
  74. }
  75.  
  76. public void exec(Session session,String command) {
  77. ChannelExec channelExec = null;
  78. try {
  79. System.out.println("Session connected.");
  80. System.out.println("Opening Channel.");
  81. Channel channel = session.openChannel("exec");
  82. channelExec = (ChannelExec) channel;
  83. channelExec.setCommand(command);
  84. channelExec.connect();
  85. channelExec.setInputStream(null);
  86. InputStream in = channelExec.getInputStream();
  87. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  88. String buf = null;
  89. while ((buf = reader.readLine()) != null)
  90. {
  91. System.out.println(buf);
  92. }
  93. reader.close();
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. channelExec = null;
  97. }finally {
  98. channelExec.disconnect();
  99. }
  100. }
  101.  
  102. /**
  103. * linux上传文件
  104. */
  105. public void upload(String directory, File file) {
  106. ChannelSftp sftp = connectSftp();
  107. try {
  108. if (null != sftp) {
  109. sftp.cd(directory);
  110. logger.info("cd {}", directory);
  111. FileInputStream stream = new FileInputStream(file);
  112. try {
  113. sftp.put(stream, file.getName());
  114. } catch (Exception e) {
  115. logger.error("upload error", e);
  116. } finally {
  117. stream.close();
  118. }
  119. }
  120. } catch (Exception e) {
  121. logger.error("upload:" + host, e);
  122. } finally {
  123. if (sftp != null) {
  124. sftp.disconnect();
  125. sftp.exit();
  126. }
  127. sshSession.disconnect();
  128. }
  129. }
  130.  
  131. /**
  132. * linux下载文件
  133. */
  134. public void get(String remotePath,String remoteFilename,String localFileName) {
  135. ChannelSftp sftp = connectSftp();
  136. File file=null;
  137. OutputStream output=null;
  138. try {
  139. if (null != sftp) {
  140. file = new File(localFileName);
  141. if (file.exists()){
  142. file.delete();
  143. }
  144. file.createNewFile();
  145. sftp.cd(remotePath);
  146.  
  147. output=new FileOutputStream(file);
  148. try {
  149. logger.info("Start download file:{},the arm file name:{}",remotePath+remoteFilename,localFileName);
  150. sftp.get(remoteFilename, output);
  151. logger.info("down {} suc", remotePath+remoteFilename);
  152. } catch (Exception e) {
  153. logger.error("download error", e);
  154. } finally {
  155. output.close();
  156. }
  157. }
  158. } catch (Exception e) {
  159. logger.error("down error :" , e);
  160. } finally {
  161. close(sftp);
  162. }
  163. }
  164.  
  165. protected void close(ChannelSftp sftp) {
  166. if (sftp != null) {
  167. sftp.disconnect();
  168. sftp.exit();
  169. }
  170. if (sshSession != null) {
  171. sshSession.disconnect();
  172. }
  173. }
  174.  
  175. }

java sftp判断目录是否存在的更多相关文章

  1. JAVA SFTP文件上传、下载及批量下载

    JavaJsch  1.jsch官方API查看地址(附件为需要的jar) http://www.jcraft.com/jsch/ 2.jsch简介 JSch(Java Secure Channel)是 ...

  2. JAVA Sftp 上传下载

    SftpUtils package xxx;import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com ...

  3. Eclipse JAVA项目的 目录结构 和 导入

    说明:本文所有测试以java工程为例: 1. Eclipse下的java工程目录 eclipse的基本工程目录叫做workspace,每个运行时的eclipse实例只能对应一个workspace,也就 ...

  4. Java命名和目录接口——JNDI

    JNDI即Java命名和目录接口(JavaNaming and Directory Interface),它属于J2EE规范范畴,是J2EE的核心技术之一,提供了一组接口.类和关于命名空间的概念.JD ...

  5. Java如何判断文件或者文件夹是否在?不存在如何创建?

    Java如何判断文件或者文件夹是否在?不存在如何创建?   1. 首先明确一点的是:test.txt文件可以和test文件夹同时存在同一目录下:test文件不能和test文件夹同时存在同一目录下. 原 ...

  6. Java遍历一个目录下的所有文件

    Java遍历一个目录下的所有文件   Java工具中为我们提供了一个用于管理文件系统的类,这个类就是File类,File类与其他流类不同的是,流类关心的是文件的内容,而File类关心的是磁盘上文件的存 ...

  7. java代码实现目录结构

    今天用java代码来实现.像我们电脑盘符那样的目录结构.在代码开始之前首先.介绍一下.用.java代码实现目录的思想. 第一步:完成基础的.大家想.我们是如何获取文件的.是不是用File类,直接就获取 ...

  8. Java SFTP 上传、下载等操作

    Java SFTP 上传.下载等操作 实际开发中用到了 SFTP 用于交换批量数据文件,然后琢磨了下这方面的东西,基于 JSch 写了个工具类记录下,便于日后使用. JSch是 SSH2 的纯Java ...

  9. 深入Java虚拟机--判断对象存活状态

    程序计数器,虚拟机栈和本地方法栈 首先我们先来看下垃圾回收中不会管理到的内存区域,在Java虚拟机的运行时数据区我们可以看到,程序计数器,虚拟机栈,本地方法栈这三个地方是比较特别的.这个三个部分的特点 ...

随机推荐

  1. org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: java.lang.NumberFormatException: For input string: "W%" ### Cause: java.lang.NumberFormatException: For input s

    一个常见的myBatis xml文件中的引号错误: org.apache.ibatis.exceptions.PersistenceException: ### Error querying data ...

  2. java_第一年_JavaWeb(11)

    自定义标签:主要是用来移除JSP页面中的java代码. 先从一个简单的案例了解其怎么移除代码: 一个正常的jsp页面: <%@ page language="java" pa ...

  3. 在react中用装饰器来装饰connect

    假设我们在react中有如下header组件: import React, { PureComponent } from 'react'; import { connect } from 'react ...

  4. 经典的最大流题POJ1273(网络流裸题)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. mysql的sql语句优化方法面试题总结

    mysql的sql语句优化方法面试题总结 不要写一些没有意义的查询,如需要生成一个空表结构: select col1,col2 into #t from t where 1=0 这类代码不会返回任何结 ...

  6. XMLHttpRequest.setRequestHeader()

    在AJAX中,如果需要像 HTML 表单那样 POST 数据,需要使用 setRequestHeader() 方法来添加 HTTP 头. 然后在 send() 方法中规定需要希望发送的数据: setR ...

  7. jsp页面随页面初始化加载js函数

    1 <%@ page language="java" import="java.util.*" pageEncoding="gbk"% ...

  8. AI-sklearn 学习笔记(一)sklearn 一般概念

    scikit-learn Machine Learning in Python Simple and efficient tools for data mining and data analysis ...

  9. .NET File 多图上传

    HTML代码: <div> <div> <input type="file" style="display:none" id=&q ...

  10. springboot集成hibernate

    package com.jxd.Boot.hibernate.dao.impl; import java.util.List; import javax.persistence.EntityManag ...