package org.test.sftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.provider.sftp.SftpClientFactory;
import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTPClient {
public String ip;
public String port;
public String username;
public String password;
private ChannelSftp command;
private Session session;
public SFTPClient(String ip, String port, String username, String password) {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
command = null;
}
public boolean connect() throws JSchException {
// If the client is already connected, disconnect
if (command != null) {
disconnect();
}
FileSystemOptions fso = new FileSystemOptions();
try {
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
fso, "no");
session = SftpClientFactory.createConnection(ip,
Integer.parseInt(port), username.toCharArray(),
password.toCharArray(), fso);
Channel channel = session.openChannel("sftp");
channel.connect();
command = (ChannelSftp) channel;
} catch (FileSystemException e) {
e.printStackTrace();
return false;
}
return command.isConnected();
}
public void disconnect() {
if (command != null) {
command.exit();
}
if (session != null) {
session.disconnect();
}
command = null;
}
protected boolean downloadFileAfterCheck(String remotePath, String localPath)
throws IOException {
FileOutputStream outputSrr = null;
try {
File file = new File(localPath);
if (!file.exists()) {
outputSrr = new FileOutputStream(localPath);
command.get(remotePath, outputSrr);
}
} catch (SftpException e) {
try {
System.err.println(remotePath + " not found in "
+ command.pwd());
} catch (SftpException e1) {
e1.printStackTrace();
}
e.printStackTrace();
return false;
} finally {
if (outputSrr != null) {
outputSrr.close();
}
}
return true;
}
protected boolean downloadFile(String remotePath, String localPath)
throws IOException {
FileOutputStream outputSrr = new FileOutputStream(localPath);
try {
command.get(remotePath, outputSrr);
} catch (SftpException e) {
try {
System.err.println(remotePath + " not found in "
+ command.pwd());
} catch (SftpException e1) {
e1.printStackTrace();
}
e.printStackTrace();
return false;
} finally {
if (outputSrr != null) {
outputSrr.close();
}
}
return true;
}
public boolean uploadFile(String localPath, String remotePath)
throws RuntimeException {
FileInputStream inputSrr = null;
try {
inputSrr = new FileInputStream(localPath);
command.put(inputSrr, remotePath);
} catch (SftpException e) {
e.printStackTrace();
throw new RuntimeException();
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
IOUtils.closeQuietly(inputSrr);
disconnect();
}
return true;
}
}
分享到:
相关推荐
android sftp上传下载
SFTP工具,文件上传下载方便。可直接文件夹传输。
sftp上传下载,里面包含jar包,方法,直接可以调用
python sftp上传下载
C# SFTP上传下载文件,此代码来源于互联网,本人整理,修改和测试无问题的代码,共享给大家。
ftp上传下载,sftp上传下载
可以从SFTP下载指定数据,使用前请设定执行参数,上传时若远程无路径可以创建,下载时若本地无路径可以创建,SFTP配置是从数据库中下载的,请自行修改。我用脑袋保证,绝对好用
java实现sftp上传下载文件,使用jcraft实现,含所需jar包。
pb8.0利用psftp.exe进行sftp上传下载,在代码中进项调用。pb调用插件进行上传下载文件
写了一个java操作sftp的工具类,里面有上传、下载、批量操作、远程创建目录、删除文件等等方法。里面的方法,都经过我测试,可用。。
该工具支持ftp和sftp的上传和下载 1
公司最近需要用到这个xml解析,然后用sftp上传和下载,这个是我自己写的一个上传下载还有xml的解析和生成,我前面的博客有具体说明,
sftp上传下载功能的实现,附带前端页面和后台代码
jsch-0.1.53.jar,实现SFTP上传下载删除,ava中通过jsch来连接远程服务器执行linux命令
通过配置文件链接sftp服务器,下载指定文件,上传指定文件,常常用于更新服务器指定资源。
JAVA实现SFTP文件传输和下载,包含事例代码
基于qt5+mingw+win7,里面包括有封装好的sftp库。参考原作者https://download.csdn.net/download/qq_26360165/10576625并修改。最简单的一个小demo
使用Putty中的psftp工具实现bat脚本上传sftp文件,如果需要定时间,可自行使用Windows任务计划程序创建定时任务达到自动上传的效果
linux脚本sftp上传文件
适合java进行文件上传下载的童鞋们,常用方法已包装好,可直接使用。