通过JSch - Java实现的SFTP
发布时间:2020-12-15 03:19:17 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.FileNotFoundException;import java.io.OutputStream;import java.net.UnknownHostException;import java.util.Properties;import jav
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.httpclient.auth.AuthenticationException;
import com.ebao.jewel.gs.integration.pub.CommonLog.exception.ExceptionUtil;
import com.ebao.jewel.gs.integration.pub.CommonLog.utils.JewelIntLogUtils;
import com.ebao.jewel.gs.integration.pub.commons.InterfaceUtil;
import com.ebao.jewel.gs.integration.pub.connection.IConnection;
import com.ebao.jewel.gs.integration.pub.constants.InterfaceConstants;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class JewelSftpClient implements IConnection {
private Session sshSession = null;
private ChannelSftp channelSftp = null;
private int connectTimes = 0;
public boolean connect(String host,int port,String username,String pwd)
throws Exception {
if (sshSession == null || !sshSession.isConnected()) {
JSch jsch = new JSch();
try {
sshSession = jsch.getSession(username,host,port);
} catch (JSchException je) {
// throw new Connection
throw je;
}
}
if (!sshSession.isConnected()) {
InterfaceUtil.batchLog("[TEST] sshSession.connect()");
sshSession.setPassword(pwd);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking","no");
sshSession.setConfig(sshConfig);
try {
sshSession.connect();
} catch (JSchException ex) {
if (ex.getMessage().indexOf("Session.connect") != -1) {
throw new UnknownHostException(ExceptionUtil.getExceptionMsg(ex));
} else {
throw new AuthenticationException(ExceptionUtil.getExceptionMsg(ex));
}
}
}
InterfaceUtil.batchLog("[TEST] sshSession has connected.");
try {
if (channelSftp == null || !channelSftp.isConnected()) {
InterfaceUtil.batchLog("[TEST] channelSftp.connect()");
channelSftp = (ChannelSftp) sshSession.openChannel("sftp");
channelSftp.connect();
}
InterfaceUtil.batchLog("[TEST] channelSftp has been established.");
return true;
} catch (JSchException je) {
if (connectTimes++ < InterfaceConstants.MAX_CONNECT_TIMES) {
String seq = connectTimes == 1 ? "1st" : connectTimes == 2
? "2nd"
: connectTimes == 3 ? "3rd" : connectTimes + "st";
InterfaceUtil.batchLog("[TEST]" + seq
+ " connection failed. Disconnected "
+ (disconnect() == true ? "succeeded" : "failed"));
long sleepTime = InterfaceUtil.getConnectSleepTime() * 1000L;
Thread.sleep(sleepTime);
InterfaceUtil.batchLog("[TEST]" + sleepTime + " ms passed. Try "
+ (int) (connectTimes + 1) + " connection.");
if (connectTimes == 4) {
InterfaceUtil
.batchLog("[TEST] !!!!!!!!!!!connection goes wrong!!!!!!!!!!!!!!!!");
}
return connect(host,port,username,pwd);
} else {
InterfaceUtil.batchLog("[TEST] connect excceeded 10 times.");
throw je;
}
}
}
public boolean disconnect() throws Exception {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.disconnect();
InterfaceUtil.batchLog("[TEST] channelSftp has cloesd.");
}
if (sshSession != null && sshSession.isConnected()) {
sshSession.disconnect();
InterfaceUtil.batchLog("[TEST] sshSession has cloesd.");
}
return true;
} catch (Exception e) {
return false;
}
}
public void upload(String src,String dst) throws Exception {
channelSftp.put(src,dst);
}
public void download(String src,String dst) throws Exception {
channelSftp.get(src,dst);
}
@SuppressWarnings("unchecked")
public Vector<Object> listFiles(String directory) throws Exception {
try {
channelSftp.cd(directory);
} catch (Exception e) {
throw new FileNotFoundException("No such file or directory.src:"
+ directory);
}
Vector<Object> files = channelSftp.lsnames(directory);
return files;
}
public boolean moveFile(String src,String dst) throws Exception {
try {
channelSftp.rename(src,dst);
return true;
} catch (Exception e) {
JewelIntLogUtils.batchLog("[TEST] Move the file from " + src + " to "
+ dst + " fail",e);
throw e;
}
}
public boolean isConnected() {
return channelSftp != null && channelSftp.isConnected();
}
@Override
public void download(String src,OutputStream out) throws Exception {
channelSftp.get(src,out);
}
}
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
