This commit is contained in:
xuwujing
2023-12-29 14:47:39 +08:00
parent a033648275
commit d26489d68f
2 changed files with 96 additions and 89 deletions

View File

@@ -223,12 +223,19 @@
</dependency> </dependency>
<!--SQL Server 驱动包 --> <!--SQL Server 驱动包 -->
<dependency> <!--<dependency>
<groupId>com.microsoft.sqlserver</groupId> <groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId> <artifactId>sqljdbc4</artifactId>
<version>4.0</version> <version>4.0</version>
</dependency> </dependency>
-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.0.jre8</version>
<scope>test</scope>
</dependency>
<!-- 数据库相关jar end --> <!-- 数据库相关jar end -->

View File

@@ -1,88 +1,88 @@
package com.pancm.ftp; //package com.pancm.ftp;
//
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import net.schmizz.sshj.SSHClient; //import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient; //import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier; //import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
//
import java.io.IOException; //import java.io.IOException;
//
//
@Slf4j //@Slf4j
public final class SmartSshUtils { //public final class SmartSshUtils {
//
public static boolean testSFTP(String hostName, // public static boolean testSFTP(String hostName,
String username, // String username,
String password){ // String password){
SSHClient ssh = new SSHClient(); // SSHClient ssh = new SSHClient();
SFTPClient sftpClient = null; // SFTPClient sftpClient = null;
try { // try {
//ssh.loadKnownHosts(); to skip host verification // //ssh.loadKnownHosts(); to skip host verification
ssh.addHostKeyVerifier(new PromiscuousVerifier()); // ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(hostName); // ssh.connect(hostName);
ssh.authPassword(username, password); // ssh.authPassword(username, password);
sftpClient = ssh.newSFTPClient(); // sftpClient = ssh.newSFTPClient();
return true; // return true;
}catch (IOException e) { // }catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
//
return false; // return false;
} // }
//
//
public static void downLoadFileBySsh(String hostName, // public static void downLoadFileBySsh(String hostName,
String username, // String username,
String password, // String password,
String srcFilePath, // String srcFilePath,
String targetFilePath // String targetFilePath
) { // ) {
SSHClient ssh = new SSHClient(); // SSHClient ssh = new SSHClient();
SFTPClient sftpClient = null; // SFTPClient sftpClient = null;
try { // try {
//ssh.loadKnownHosts(); to skip host verification // //ssh.loadKnownHosts(); to skip host verification
ssh.addHostKeyVerifier(new PromiscuousVerifier()); // ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(hostName); // ssh.connect(hostName);
ssh.authPassword(username, password); // ssh.authPassword(username, password);
sftpClient = ssh.newSFTPClient(); // sftpClient = ssh.newSFTPClient();
sftpClient.get(srcFilePath, targetFilePath); // sftpClient.get(srcFilePath, targetFilePath);
//create a folder // //create a folder
// sftpClient.mkdir("/opt/app/testFolder"); //// sftpClient.mkdir("/opt/app/testFolder");
//sftpClient.mkdirs("");创建多级文件夹 // //sftpClient.mkdirs("");创建多级文件夹
//sftpClient.rmdir("");重命名文件夹 // //sftpClient.rmdir("");重命名文件夹
//sftpClient.ls(""); //列出当前目录 // //sftpClient.ls(""); //列出当前目录
} catch (IOException e) { // } catch (IOException e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
} finally { // } finally {
if (null != sftpClient) { // if (null != sftpClient) {
try { // try {
sftpClient.close(); // sftpClient.close();
} catch (IOException e) { // } catch (IOException e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
} // }
} // }
try { // try {
ssh.disconnect(); // ssh.disconnect();
} catch (IOException e) { // } catch (IOException e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
} // }
} // }
} // }
//
/** // /**
* 静态工具类应该禁用构造方法 // * 静态工具类应该禁用构造方法
*/ // */
private SmartSshUtils(){} // private SmartSshUtils(){}
//
//
public static void main(String[] args) { // public static void main(String[] args) {
String hostName="192.168.9.80"; // String hostName="192.168.9.80";
String username="root"; // String username="root";
String password="Admin#12$34!"; // String password="Admin#12$34!";
String srcFilePath="/home/release/file"; // String srcFilePath="/home/release/file";
String targetFilePath="D:\\d1"; // String targetFilePath="D:\\d1";
//
SmartSshUtils.downLoadFileBySsh(hostName,username,password,srcFilePath,targetFilePath); // SmartSshUtils.downLoadFileBySsh(hostName,username,password,srcFilePath,targetFilePath);
//
} // }
} //}