如何使用jcraft 模拟SFTP登陆
大家如果熟悉Linux系统话,对ssh,sftp,scp等命令非常熟悉。ssh是一个安全协议,用来在不同系统或者服务器之间进行安全连接。ssh 在连接和传送的过程中会加密所有的数据。
而今天我要介绍的一个jar包,是使用 JSCH。JSCH是一个纯粹的用Java实现SSH功能的java library.
官方地址为:http://www.jcraft.com/jsch/
GitHub 地址为:https://github.com/vngx/vngx-jsch
maven配置如下:
<!-- 加入sftp依赖包 -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>
对于这样的设计我们先设计一个FTP连接接口:
package com.xuanyuan.tools; import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
/**
* FTP 接口
* @author Punk Lin
* @email lentr@sina.cn
* @date 2016年12月23日
*
*/
public interface FTPService { /**
* 登入
* @param host 登入主机地址
* @param userName 用户名
* @param password 密码
* @param port 端口
* @throws Exception
*/
public void login(String host, String userName, String password, int port) throws Exception; /**
* 退出
*/
public void logout(); /**
* 上传文件
*
* @param in
* 输入流
* @param remoteFilePath
* 远程文件绝对路径
* @throws Exception
*/
public void uploadFile(InputStream in, String remoteFilePath) throws Exception; /**
* 文件下载到本地
*
* @param sourceFilePath
* 远程文件绝对路径
* @param localFilePath
* 本地目录或绝对路径
* @throws Exception
*/
public void downloadFile(String sourceFilePath, String localFilePath)
throws Exception; /**
* 文件下载到输出流
*
* @param sourceFilePath
* 远程文件绝对路径
* @param out
* 输出流
* @throws Exception
*/
public void downloadFile(String sourceFilePath, OutputStream out) throws Exception; /**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
*
* @throws Exception
*/
public void deleteFile(String directory, String fileName) throws Exception; /**
* 列出目录下的文件,包括目录
*
* @param directory
* 要列出的目录
*
* @return list 文件名列表
*
* @throws Exception
*/
public Vector<?> listFiles(String directory) throws Exception; }
对于它的具体实现,则如下:
package com.xuanyuan.tools.impl; import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Vector; import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.xuanyuan.tools.FTPService; public class SFTPServiceImpl implements FTPService {
/** sftp会话session */
protected Session sshSession = null; /** sftp通道 */
protected ChannelSftp sftp = null; @Override
public void login(String host, String userName, String password, int port)
throws Exception {
try {
JSch jsch = new JSch();
this.sshSession = jsch.getSession(userName, host, port);
this.sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
this.sshSession.setConfig(sshConfig);
this.sshSession.connect(20000);
this.sftp = (ChannelSftp) sshSession.openChannel("sftp");
this.sftp.connect();
this.sftp.setFilenameEncoding("UTF-8");
} catch (JSchException e) {
throw new RuntimeException("无法使用sftp登陆,请检查用户名密码或端口");
} } @Override
public void logout() {
if (this.sftp != null) {
this.sftp.disconnect();
this.sshSession.disconnect();
this.sftp = null;
this.sshSession = null;
}
} @Override
public void uploadFile(InputStream in, String remoteFilePath)
throws Exception {
try {
String filepath = remoteFilePath.substring(0,
remoteFilePath.lastIndexOf("/"));
String fileName = remoteFilePath.substring(
remoteFilePath.lastIndexOf("/") + 1,
remoteFilePath.length());
this.sftp.cd(filepath);
this.sftp.put(in, fileName, 0); } catch (SftpException e) {
throw new RuntimeException("上传文件时发生错误!请检查文件路径是否正确");
} } @Override
public void downloadFile(String sourceFilePath, String localFilePath)
throws Exception {
File file = new File(localFilePath);
FileOutputStream out = null;
try {
if (file.isDirectory()) {
this.sftp.get(sourceFilePath, localFilePath);
} else {
out = new FileOutputStream(file);
this.sftp.get(sourceFilePath, out);
}
} catch (SftpException e) {
throw new RuntimeException("下载文件时发生错误!请检查文件路径是否正确");
} finally {
if (out != null)
out.close();
}
} @Override
public void downloadFile(String sourceFilePath, OutputStream out)
throws Exception {
try {
this.sftp.get(sourceFilePath, out);
} catch (SftpException e) {
throw new RuntimeException("下载文件时发生错误!请检查文件路径是否正确");
} } @Override
public void deleteFile(String directory, String fileName) throws Exception {
this.sftp.cd(directory);
this.sftp.rm(fileName); } @Override
public Vector<?> listFiles(String directory) throws Exception {
return this.sftp.ls(directory);
} }
至此工具完成了。这样我们也可以通过这个工具提供,网页这样的上传道服务器。
如何使用jcraft 模拟SFTP登陆的更多相关文章
- 模拟Post登陆带验证码的网站
前言: 作者在一个项目需求 模拟用户登陆,获取该用户的订单记录. 该系统需要用户名,密码,验证码 (验证码为正楷的数字4位),于是参考网络一些文章,并进行了很多测试,总结步骤如下: 步骤1 : 通过h ...
- curl模拟自动登陆&采集网页数据
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- python urllib2 模拟网站登陆
python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...
- HttpClient4的使用,模拟浏览器登陆新浪微博,发表微博和文字+图片微博
HttpClient4,最原始的需求就是使用其来模拟浏览器想服务器发起http请求,当然,他的功能不止于此,但是我需要的就是这个功能而已,jdk也有其自带的类似的api:UrlConnection,效 ...
- Java模拟新浪微博登陆抓取数据
前言: 兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二. 首先: 要想登陆新浪微博需要 ...
- Python模拟微博登陆,亲测有效
今天想做一个微博爬个人页面的工具,满足一些不可告人的秘密.那么首先就要做那件必做之事!模拟登陆-- 代码是参考了:https://www.douban.com/note/201767245/ 我对代码 ...
- C# 模拟网站登陆
实现此功能首先需要借助一些抓包工具,对相应的网站登陆过程进行分析,此过程根据网站的不同,可能复杂,也可能很简单.常用的抓包工具FF下FireBug和IE下的HttpWatch.这两个工具很强大,以此工 ...
- Golang模拟用户登陆,突破教务系统
目录 一.Golang模拟用户登陆,突破教务系统 1.1 请求登陆页面 1.2 抓包分析登陆请求 1.3 golang使用js引擎合成salt 1.4 模拟表单提交,完成登陆 1.5 进入成绩查询页, ...
- [Javascript] 爬虫 模拟新浪微博登陆
概述: 由于业务需要,要编写爬虫代码去爬去新浪微博用户的信息. 虽然在网上能找到不少信息,但由于新浪微博改版,其登陆机制进行了修改,故很多老的文章就不适合用了. 经过一番摸索,成功模拟新浪微博的登陆 ...
随机推荐
- python学习心得第四章
python 学习心得第四章 1.lambda表达式 1:什么是lambda表达式 为了简化简单函数的代码,选择使用lambda表达式 上面两个函数的表达式虽然不一样,但是本质是一样的,并且lamb ...
- matlab struct结构体用法
结构体的赋值: 结构体的赋值,这里不建议用下面这种形式进行统一赋值, s = sturct('field1',values1,'field2',values2,-) 而是建议直接赋值,就是对每一个属性 ...
- vim 标记 mark 详解 (转载)
http://www.cnblogs.com/jianyungsun/archive/2011/02/14/1954057.html Vim 允许你在文本中放置自定义的标记.命令 "ma&q ...
- 集合框架之——迭代器并发修改异常ConcurrentModificationException
问题: 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. 使用普通迭代器出现的异常: ...
- CUDA代码的高亮设置
以下基于"WIN7(64位)+Visual Studio 2010+CUDA7.5". 语法高亮除了看起来舒服之外,还可以使用F11寻找函数.变量定义,输入函数的时候也会有相应的提 ...
- nginx https反向代理 tomcat
前端nginx配置 server { listen ; server_name localhost; listen ssl; ssl_certificate /usr/local/nginx/sslk ...
- angular+ionic返回上一页并刷新
假定当前页面为editCata页面,要返回的是cataDetail页面.目前我找到两种方法实现返回上一页并刷新,如果以后有其它方法,再继续添加. 1.在editCataCtrl.js中注入$ionic ...
- .net妹纸转Java---java环境的搭建,myeclipse10.0 的安装环境变量配置和破解
啦啦啦 ,因为公司项目需要,从我大火炉--大武汉被拖到了更大的火炉--大广西 其实一开始 我的内心是拒绝的. 但是我在大武汉呆了近2年木有出过远门,对, 生活除了眼前的苟且,还有远方的苟且.怀揣这样 ...
- 记一次u盘 无法格式化、0字节、写保护的解决过程
首先各种找方法,下载了一堆烂七八糟的东西都没能解决 后来看了这个链接的文章 http://jingyan.baidu.com/article/6079ad0e5bdec428ff86dbcd.html ...
- [z]查找锁表并解锁
select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_mode f ...