由于公司需要,在网上找了一下支持SFTP的包,发现俩个不错的,一个是 Secure FTP Factory ,一个是 J2SSH Maverick ,但都是收费的,现在简单介绍一下 J2SSH Maverick 怎样简单使用。代码如下:
/* HEADER */
import com.maverick.ssh.*;
import com.maverick.ssh2.*;
import java.io.*;
import com.sshtools.net.*;
import com.sshtools.sftp.*;
/**
* This example demonstrates the connection process connecting to an SSH2 server
* and usage of the SFTP client.
*/
public class MySftpConnect {
public static void main(String[] args) {
MySftpConnect c = new MySftpConnect();
c.createSftpClient("192.168.0.108", "szb", "szbpatternx86", 22);--参数依次为主机名,用户名,密码,端口号。
}
public void createSftpClient(String hostname, String username,
String password, int port) {
try {
System.out.println("Connecting to " + hostname);
// Create an SshConnector instance
SshConnector con = SshConnector.getInstance();
// Connect to the host
SocketTransport t = new SocketTransport(hostname, port);
t.setTcpNoDelay(true);
SshClient ssh = con.connect(t, username);
Ssh2Client ssh2 = (Ssh2Client) ssh;
// Authenticate the user using password authentication
com.maverick.ssh.PasswordAuthentication pwd = new com.maverick.ssh.PasswordAuthentication();
do {
pwd.setPassword(password);
} while (ssh2.authenticate(pwd) != SshAuthentication.COMPLETE
&& ssh.isConnected());
// Start a session and do basic IO
if (ssh.isAuthenticated()) {
SftpClient sftp = new SftpClient(ssh2);
// test create file.
this.createTestFile(sftp);
}
} catch (Throwable th) {
th.printStackTrace();
}
}
//测试IO操作
public void createTestFile(SftpClient sftp) throws Exception {
File textFile = new File(System.getProperty("user.home"), "shining.txt");
FileOutputStream tout = new FileOutputStream(textFile);
// Create a file with \r\n as EOL
for (int i = 0; i < 100; i++) {
tout.write("All work and no play makes Jack a dull boy中文\r\n"
.getBytes());
}
tout.close();
// Perform some text mode operations指定文件存储为txt类型
sftp.setTransferMode(SftpClient.MODE_TEXT);
// Tell the client which EOL the remote client is using - note
// that this will be ignored with version 4 of the protocol
sftp.setRemoteEOL(SftpClient.EOL_LF);
//将文件上传到服务器
sftp.put(textFile.getAbsolutePath());
}
}
写的不太详细,请参考:http://3sp.com/kb/idx/0/014/article/Getting_started_with_J2SSH_Maverick.html
相关推荐
sftp协议ssh.txt sftp协议ssh.txt sftp协议ssh.txt
Delphi通过SFTP连接Linux服务器,有利于做数据的对接
免费支持搭建SFTP和FTP服务器,及操作说明,可以通过该操作手册实现在windows下实现SFTP或FTP服务器搭建!
基于SSH协议的sftp协议,用来当作FTP下载上传文件,但是会有乱码问题
前端开源库-ssh2-sftp-clientssh2 sftp客户端,节点的ssh2 sftp客户端
连接SSH远程服务器,SFTP、FTP工具类。IFTP为接口,接口基本满足日常项目需要,派生MyFTPClient、MySFTPClient,方便应用扩展及整合到项目,提供例子拿来即用。sftp基于sshNet实现、Renci.SshNet是目前最为强大的C#...
主要介绍了详解Java使用Jsch与sftp服务器实现ssh免密登录,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
自定义SFTP类 libssh2-1.6.0
put(src,remotePath,options)==>字符串fastPut(localPath,remotePath,options)==>字符串append(input,remotePath,options)==>字符串mkdir(路径,递归)==>字符串rmdir(路径,递归)==>字符串delete...
在java里可以通过Ganymed SSH-2, Expect4j等实现ssh登录,由于Ganymed SSH-2是纯java实现,并且可以实现scp,sftp等,因此我们采用Ganymed SSH-2 1> 下载安装 从http://code.google.com/p/ganymed-ssh-2/ 下载...
ftp sftp协议上传文件区别,具体代码实现,可以参考很好的代码材料
最近项目要求数据传必须使用sftp,但sftp又不能直接使用22端口,不安全,网上查询的ssh和sftp分离相关的资料都是基于centos7的,于是经过研究尝试,完成了基于centos6版本的服务分离
Java运用ganymed-ssh2-build210.jar包远程连接操作linux服务器 (SCP和SFTP的运用)
windows下C语言实现的通过ssh2连接设备sftp传文件并执行 1.所有设备的IP地址保存在本地文件TXT中 2.取IP做成一个链表 3。取结点 4.创建容量为100的线程池,ssh2连接设备,sftp传输文件并执行 5.返回结果写入本地文件...
ssh连接慢解决办法(成功) 用真机连接虚拟机卡的话: 1.进入虚拟机vim /etc/ssh/sshd_config 2.将 #UseDNS yes改为UseDNS no即可如下: 使用/UseDNS找到地方然后添加 #UseDNS yes UseDNS no #PidFile /var/run/sshd....
sftp - 这个Go包实现了SSH文件传输协议
cuteftp使用ssh2连接sftp时候报错:缺少组件。 CUTEFTP缺失文件sftp21.dll 状态:> [2012/8/1 0:27:48] 正在获取清单 “”… 状态:> [2012/8/1 0:27:48] 正在初始化 SFTP21 模块… 错误:> [2012/8/1 0:27:48] 无法...
grunt-ssh, SSH,Grunt的SFTP任务 新主人 启动 12 -23-2015,我正站在两个巨人( @chuckmo和 @andrewrjones) 作为这个项目维护者的肩膀上) 。 欢迎捐赠,如往常。 ( 这里消息也将在下一次发布时删除) 。静音 ssh
SFTP例子和jsch-0.1.44.jar
sftp-get-ssh-插件 查看详细信息