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

View File

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