SFTP上传下载

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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值