This commit is contained in:
yuqh 2025-01-07 09:25:02 +08:00
parent 435665c944
commit ebf26cf97f
2 changed files with 104 additions and 104 deletions

View File

@ -5,13 +5,13 @@ import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import org.apache.commons.net.ftp.FTPClient;
//import com.jcraft.jsch.Channel;
//import com.jcraft.jsch.ChannelSftp;
//import com.jcraft.jsch.JSch;
//import com.jcraft.jsch.Session;
//import com.jcraft.jsch.SftpATTRS;
//import com.jcraft.jsch.SftpException;
//import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@ -107,8 +107,8 @@ public class BackUpDatabaseInitializer extends PluginBaseEntity {
private String sftpFilePase;
private ChannelSftp sftp = null;
private Session sshSession = null;
// private ChannelSftp sftp = null;
// private Session sshSession = null;
@Override
public JsonResultEntity executeBusiness(JSONObject requestJson) {
@ -224,54 +224,54 @@ public class BackUpDatabaseInitializer extends PluginBaseEntity {
}
public void connect() {
try {
JSch jsch = new JSch();
jsch.getSession(sftpUsername, sftpHost, sftpPort);
sshSession = jsch.getSession(sftpUsername, sftpHost, sftpPort);
if (logger.isInfoEnabled()) {
logger.info("Session created.");
}
sshSession.setPassword(sftpPassword);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
if (logger.isInfoEnabled()) {
logger.info("Session connected.");
}
Channel channel = sshSession.openChannel("sftp");
channel.connect();
if (logger.isInfoEnabled()) {
logger.info("Opening Channel.");
}
sftp = (ChannelSftp) channel;
if (logger.isInfoEnabled()) {
logger.info("Connected to " + host + ".");
}
} catch (Exception e) {
}
// try {
// JSch jsch = new JSch();
// jsch.getSession(sftpUsername, sftpHost, sftpPort);
// sshSession = jsch.getSession(sftpUsername, sftpHost, sftpPort);
// if (logger.isInfoEnabled()) {
// logger.info("Session created.");
// }
// sshSession.setPassword(sftpPassword);
// Properties sshConfig = new Properties();
// sshConfig.put("StrictHostKeyChecking", "no");
// sshSession.setConfig(sshConfig);
// sshSession.connect();
// if (logger.isInfoEnabled()) {
// logger.info("Session connected.");
// }
// Channel channel = sshSession.openChannel("sftp");
// channel.connect();
// if (logger.isInfoEnabled()) {
// logger.info("Opening Channel.");
// }
// sftp = (ChannelSftp) channel;
// if (logger.isInfoEnabled()) {
// logger.info("Connected to " + host + ".");
// }
// } catch (Exception e) {
// }
}
/**
* 关闭连接
*/
public void disconnect() {
if (this.sftp != null) {
if (this.sftp.isConnected()) {
this.sftp.disconnect();
if (logger.isInfoEnabled()) {
logger.info("sftp is closed already");
}
}
}
if (this.sshSession != null) {
if (this.sshSession.isConnected()) {
this.sshSession.disconnect();
if (logger.isInfoEnabled()) {
logger.info("sshSession is closed already");
}
}
}
// if (this.sftp != null) {
// if (this.sftp.isConnected()) {
// this.sftp.disconnect();
// if (logger.isInfoEnabled()) {
// logger.info("sftp is closed already");
// }
// }
// }
// if (this.sshSession != null) {
// if (this.sshSession.isConnected()) {
// this.sshSession.disconnect();
// if (logger.isInfoEnabled()) {
// logger.info("sshSession is closed already");
// }
// }
// }
}
/**
@ -284,23 +284,23 @@ public class BackUpDatabaseInitializer extends PluginBaseEntity {
* @return
*/
public boolean uploadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {
FileInputStream in = null;
try {
createDir(remotePath);
File file = new File(localPath + File.separator + localFileName);
in = new FileInputStream(file);
sftp.put(in, remoteFileName, 65536);
return true;
} catch (FileNotFoundException e) {
} catch (SftpException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}
// FileInputStream in = null;
// try {
// createDir(remotePath);
// File file = new File(localPath + File.separator + localFileName);
// in = new FileInputStream(file);
// sftp.put(in, remoteFileName, 65536);
// return true;
// } catch (FileNotFoundException e) {
// } catch (SftpException e) {
// } finally {
// if (in != null) {
// try {
// in.close();
// } catch (IOException e) {
// }
// }
// }
return false;
}
@ -311,32 +311,32 @@ public class BackUpDatabaseInitializer extends PluginBaseEntity {
* @return
*/
public boolean createDir(String createpath) {
try {
if (isDirExist(createpath)) {
this.sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString())) {
sftp.cd(filePath.toString());
} else {
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
}
}
this.sftp.cd(createpath);
return true;
} catch (SftpException e) {
}
// try {
// if (isDirExist(createpath)) {
// this.sftp.cd(createpath);
// return true;
// }
// String pathArry[] = createpath.split("/");
// StringBuffer filePath = new StringBuffer("/");
// for (String path : pathArry) {
// if (path.equals("")) {
// continue;
// }
// filePath.append(path + "/");
// if (isDirExist(filePath.toString())) {
// sftp.cd(filePath.toString());
// } else {
// // 建立目录
// sftp.mkdir(filePath.toString());
// // 进入并设置为当前目录
// sftp.cd(filePath.toString());
// }
//
// }
// this.sftp.cd(createpath);
// return true;
// } catch (SftpException e) {
// }
return false;
}
@ -348,15 +348,15 @@ public class BackUpDatabaseInitializer extends PluginBaseEntity {
*/
public boolean isDirExist(String directory) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
// try {
// SftpATTRS sftpATTRS = sftp.lstat(directory);
// isDirExistFlag = true;
// return sftpATTRS.isDir();
// } catch (Exception e) {
// if (e.getMessage().toLowerCase().equals("no such file")) {
// isDirExistFlag = false;
// }
// }
return isDirExistFlag;
}

View File

@ -1,5 +1,5 @@
server:
port: 8901
port: 9999
servlet:
context-path: /kangarooDataCenterV3
localIP: 127.0.0.1