(曾在天涯)的文章详细讲解了jsch中的函数以及用法

下面是一个例子:

package com.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Properties;

import java.util.Vector;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

import com.jcraft.jsch.SftpException;

import com.jcraft.jsch.ChannelSftp.LsEntry;

public class Jsch4Sftp {

/**

* 连接sftp服务器

*

* @param host

*            主机

* @param port

*            端口

* @param username

*            用户名

* @param password

*            密码

* @return

*/

public ChannelSftp connect(String host, int port, String username,

String password) {

ChannelSftp sftp = null;

try {

JSch jsch = new JSch();

jsch.getSession(username, host, port);

Session sshSession = jsch.getSession(username, host, port);

System.out.println("Session created.");

sshSession.setPassword(password);

Properties sshConfig = new Properties();

sshConfig.put("StrictHostKeyChecking", "no");

sshSession.setConfig(sshConfig);

sshSession.connect();

System.out.println("Session connected.");

System.out.println("Opening Channel.");

Channel channel = sshSession.openChannel("sftp");

channel.connect();

sftp = (ChannelSftp) channel;

// sshSession.disconnect();

System.out.println("Connected to " + host + ".");

} catch (Exception e) {

}

return sftp;

}

/**

* 上传文件

*

* @param directory

*            上传的目录

* @param uploadFile

*            要上传的文件

* @param sftp

*/

public void upload(String directory, String uploadFile, ChannelSftp sftp) {

try {

sftp.cd(directory);

File file = new File(uploadFile);

sftp.put(new FileInputStream(file), file.getName());

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 下载文件

*

* @param directory

*            下载目录

* @param downloadFile

*            下载的文件

* @param saveFile

*            存在本地的路径

* @param sftp

*/

public void download(String directory, String downloadFile,

String saveFile, ChannelSftp sftp) {

try {

sftp.cd(directory);

File file = new File(saveFile);

sftp.get(downloadFile, new FileOutputStream(file));

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 删除文件

*

* @param directory

*            要删除文件所在目录

* @param deleteFile

*            要删除的文件

* @param sftp

*/

public void delete(String directory, String deleteFile, ChannelSftp sftp) {

try {

sftp.cd(directory);

sftp.rm(deleteFile);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 列出目录下的文件

*

* @param directory

*            要列出的目录

* @param sftp

* @return

* @throws SftpException

*/

public Vector listFiles(String directory, ChannelSftp sftp)

throws SftpException {

return sftp.ls(directory);

}

public static void main(String[] args) {

Jsch4Sftp sf = new Jsch4Sftp();

String host = "***";

int port = 22;

String username = "***";

String password = "***";

String directory = "/home";

String saveFile = "1.zip";

String downLoadDirectory = "D:\\";

ChannelSftp sftp = null;

Session sshSession = null;

try {

JSch jsch = new JSch();

jsch.getSession(username, host, port);

sshSession = jsch.getSession(username, host, port);

System.out.println("Session created.");

sshSession.setPassword(password);

Properties sshConfig = new Properties();

sshConfig.put("StrictHostKeyChecking", "no");

sshSession.setConfig(sshConfig);

sshSession.connect();

System.out.println("Session connected.");

System.out.println("Opening Channel.");

Channel channel = sshSession.openChannel("sftp");

channel.connect();

sftp = (ChannelSftp) channel;

System.out.println("Connected to " + host + ".");

//          sf.upload(directory, uploadFile, sftp);

//          sf.download(directory, downloadFile, saveFile, sftp);

//      sf.delete(directory, deleteFile, sftp);

//          sftp.cd(directory);

Vector v = sf.listFiles(directory, sftp);

for (LsEntry e : v) {

if(!e.getFilename().startsWith(".")) {

saveFile = downLoadDirectory + e.getFilename();

sf.download(directory, e.getFilename(), saveFile, sftp);

}

}

System.out.println("finished");

} catch (Exception e) {

e.printStackTrace();

} finally {

sftp.exit();

sshSession.disconnect();

}

}}

java jsch sftp_java-jsch实现sftp文件操作相关推荐

  1. JAVA ftps设置_Java使用JSCH实现对FTPS服务器文件操作

    SFTP是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.SFTP 为 SSH的一部份,是一种传输文件到服务器的安全方式.SF ...

  2. java底层 文件操作,java底层是怎的对文件操作的

    一.序言 IO操作,才程序中比较普遍,JAVA 中提出了IO/NIO 的概念,也一直在说NIO 比IO快,一直不知道原因,就想memcache 和ehcache 比较优劣一样,这些东西得自己看看如何实 ...

  3. JAVA SE学习day_03:包装类、文件操作——file

    一.包装类 包装类是为了解决基本类型不能直接参与面向对象开发的问题,实际就是将基本类型以"对象"的形式表示 Integer 1.1基本类型转换为对应的包装类(引用类型) int i ...

  4. Selenium+java - 借助autolt完成上传文件操作

    写在前面: 上传文件是每个自动化测试同学会遇到,而且可以说是面试必考的问题,标准控件我们一般用sendkeys()就能完成上传,但是我们的测试网站的上传控件一般为自己封装的,用传统的上传已经不好用了, ...

  5. java第九章IO流与文件操作

    使用输出流将String保存到文件(byte数组)byte[] bytes = "Java数据交流管道--IO流".getBytes(); 使用Filewriter 文章目录 9- ...

  6. Java基础 -IO入门简介、文件操作、与IO流分类简述

    文章目录 IO流入门概念 文件概念 什么是文件 文件流 文件常用操作 创建文件对象相关构造器和方法 创建文件案例 注意 获取文件相关信息 目录的操作和文件删除 delete mkdir.mkdirs ...

  7. 【Java基础】IO流与文件操作的一些手写代码面试题

    概述 代码 概述 IO流是我们最基础的数据操作,最近我身边的一些朋友经常遇到这样的面试题,有的居然还是手写代码,真的太残酷了:不过话说回来,IO流确实是编程里最最基础的操作了,需要我们好好掌握,熟记于 ...

  8. Java学习笔记6.3.4 文件操作 - Path接口和Files工具类

    文章目录 零.本讲学习目标 一.NIO概述 (一)NIO定义 (二)NIO相关包 (三)NIO三大核心 二.NIO.2概述 (一)NIO.2定义与特点 (二)NIO.2提供的接口与工具类 三.Path ...

  9. java中文件操作的工具类

    代码: package com.lky.pojo;import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...

最新文章

  1. JRuby:谁说鱼与熊掌不可兼得
  2. 上次谁说要简历模板来着?来!
  3. 中国太阳能热水器市场营销模式探析与品牌格局调研报告2022版
  4. VTK修炼之道35:边缘检测_Laplace算子
  5. 问题 F: 序列操作Ⅱ(前缀最大公约数,后缀最大公约数)
  6. DataOutputStream 类 和DatainputStream类 的主要方法简单介绍,及代码演示。
  7. Spark: Structured + hive(Jdbc方式)卡死
  8. 删除java速度变慢_Java正则表达式运行速度很慢
  9. 母函数 经典题目汇总
  10. vs2017 visual studio2017 密钥 激活码
  11. 现在的国产深度deepin操作系统不赖,推荐试用
  12. CentOS7.6 安装Oracle12C(上)
  13. AI赋能的判定机制的倾向性
  14. Python-并发、并行、协成的简易理解
  15. 论文笔记:SRF(stereo radiance fileds)
  16. LSCP: Locally Selective Combination in Parallel Outlier Ensembles-学习笔记
  17. 前端吃香还是后端吃香?
  18. matlab 215卷积码,基于matlab的卷积码的分析与应用
  19. golang算法之鬼谷子问徒
  20. 飞控FirmamentAutopilot介绍

热门文章

  1. Spring、Mybatis笔记
  2. 安全漏洞修复-Common FileUpload-CVE-2016-100031
  3. 数据运营:品牌“双十一”如何数据驱动怎么做?
  4. EasyCode的Mybatis终极版模板
  5. 渤海大潮_2020年云大潮–为什么这么多小型企业采用云
  6. Android自定义view之太极图
  7. 4. sql 语句中常用命令
  8. 离散数学(九):集合的基本概念和运算
  9. Activiti初识
  10. 分享几个学英语的好工具