search
HomeJavajavaTutorialHow to use Hutool's JschUtil in springboot

How to use Hutool's JschUtil in springboot

May 11, 2023 am 11:07 AM
springboothutool

windows server 2012 R2 install openssh

windows install ssh

linux itself uses ssh without installation

1. Download

Visit the Openssh official website and follow the instructions Select the appropriate installation package according to the number of bits of the operating system

How to use Hutools JschUtil in springboot

After entering the official website, select the appropriate installation package according to the number of bits of the operating system. However, 64-bit systems can also support 32-bit installation packages. For the 64-bit system here, I installed the 32-bit installation package.

2. Unzip the downloaded installation package to the C:/Program Files/ directory

Open the powershell terminal and enter the folder containing the ssh executable exe file cd C:\OpenSSH-Win32 \OpenSSH-Win32.

How to use Hutools JschUtil in springboot

3. Install ssh service

Enter the following command in the powershell terminal:

 powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

After successful installation, the following will appear:

How to use Hutools JschUtil in springboot

4. Configure ssh service

  • Open port 22 in the firewall: Enter the following command in powershell:

netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22

How to use Hutools JschUtil in springboot

Start ssh service

net start sshd

5. Add environment system variables

How to use Hutools JschUtil in springboot

will contain ssh Add the path to the executable exe file (in my case: C:\Program Files\OpenSSH-Win32\OpenSSH-Win32) to the environment system variables.

How to use Hutools JschUtil in springboot

Finally open cmd or powershell, enter ssh and the following image appears, which means the installation has been successful.

How to use Hutools JschUtil in springboot

6. Set the sshd service to start automatically at boot

Open "Server Manager" in sequence -> "Tools" - "Service"

How to use Hutools JschUtil in springboot

Enter the service list interface and find the OpenSSH SSH Server service

How to use Hutools JschUtil in springboot

Change the openssh authentication agent in the picture below Do the same as in the picture above.

How to use Hutools JschUtil in springboot

Windows that come with their own ssh service (such as windows 10) enable the ssh service

1. Client installation

Start-> Applications and Features-> Optional Features-> Add Features

There is an option for OpenSSH client in the list

Click to install OpenSSH client

After installation, you can use Windows PowerShell Directly use the ssh command

2. Server installation

Start-> Applications and Features-> Optional Features-> Add Features

There is an OpenSSH server in the list Options

Click to install the OpenSSH server

After the server is installed, you need to perform some configuration

3. Server configuration

Run Windows as administrator PowerShell

Enable SSHD service

Start-Service sshd

Set the service to start automatically

Set-Service -Name sshd -StartupType 'Automatic'

Confirm whether the firewall is open

Get-NetFirewallRule -Name *ssh*

Check whether OpenSSH-Server-In-TCP is enabled After the configuration is completed for True

, other clients can use ssh to connect to windows. The username and password are the username and password of windows

springboot uses

Introducing hutool

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.4</version>
</dependency>

Introduce jsch

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

Usage

Upload
@Test
void test18() {
//测试目录
    String linuxPath = "/var/file/test/";
    //创建session连接
    Session sessionLinux = JschUtil.getSession("106.12.127.40", 22,"root", "Ocean@123");
// 1.文件操作
    // 建立sftp
    Sftp sftp = JschUtil.createSftp(sessionLinux);
    //进入输入目录
    sftp.cd(linuxPath);
    //文件名称
    String fileName = "Sftp创建文件夹于"+DateUtil.format(new Date(), "yyyy年MM月dd日HH时mm分ss秒");
    //1.创建文件夹
    sftp.mkdir(fileName);
    System.out.println("=========================1.远程文件操作=========================");
    System.out.println(fileName+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    //删除文件
    sftp.delDir(linuxPath+fileName);
    System.out.println(fileName+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    System.out.println();
//2.上传文件
    // 本地新建文件
    System.out.println("=========================2.上传文件操作=========================");
    String localFile = DateUtil.format(new Date(), "yyyy年MM月dd日HH时mm分ss秒")+".txt";
    System.out.println(localFile);
    FileWriter fileWriter = new FileWriter(localFile);
    // 写入内容
    File file = fileWriter.write("123");
    fileWriter.append("追加信息");
    System.out.println(file.getPath());
    sftp.upload(linuxPath, file);
    //upload方法
    System.out.println("1.upload方法");
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+localFile));
    sftp.delFile(linuxPath+localFile);
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    sftp.put(file.getPath(),linuxPath);
    //put方法
    System.out.println("2.put方法");
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+localFile));
    sftp.delFile(linuxPath+localFile);
    System.out.println(localFile+",文件是否存在?"+sftp.exist(linuxPath+fileName));
    //删除本地文件
    FileUtil.del(file);
}

Running results:

How to use Hutools JschUtil in springboot

The above is the detailed content of How to use Hutool's JschUtil in springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does the JVM manage garbage collection across different platforms?How does the JVM manage garbage collection across different platforms?Apr 28, 2025 am 12:23 AM

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Why can Java code run on different operating systems without modification?Why can Java code run on different operating systems without modification?Apr 28, 2025 am 12:14 AM

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

Describe the process of compiling and executing a Java program, highlighting platform independence.Describe the process of compiling and executing a Java program, highlighting platform independence.Apr 28, 2025 am 12:08 AM

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.

How does the underlying hardware architecture affect Java's performance?How does the underlying hardware architecture affect Java's performance?Apr 28, 2025 am 12:05 AM

Java performance is closely related to hardware architecture, and understanding this relationship can significantly improve programming capabilities. 1) The JVM converts Java bytecode into machine instructions through JIT compilation, which is affected by the CPU architecture. 2) Memory management and garbage collection are affected by RAM and memory bus speed. 3) Cache and branch prediction optimize Java code execution. 4) Multi-threading and parallel processing improve performance on multi-core systems.

Explain why native libraries can break Java's platform independence.Explain why native libraries can break Java's platform independence.Apr 28, 2025 am 12:02 AM

Using native libraries will destroy Java's platform independence, because these libraries need to be compiled separately for each operating system. 1) The native library interacts with Java through JNI, providing functions that cannot be directly implemented by Java. 2) Using native libraries increases project complexity and requires managing library files for different platforms. 3) Although native libraries can improve performance, they should be used with caution and conducted cross-platform testing.

How does the JVM handle differences in operating system APIs?How does the JVM handle differences in operating system APIs?Apr 27, 2025 am 12:18 AM

JVM handles operating system API differences through JavaNativeInterface (JNI) and Java standard library: 1. JNI allows Java code to call local code and directly interact with the operating system API. 2. The Java standard library provides a unified API, which is internally mapped to different operating system APIs to ensure that the code runs across platforms.

How does the modularity introduced in Java 9 impact platform independence?How does the modularity introduced in Java 9 impact platform independence?Apr 27, 2025 am 12:15 AM

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

What is bytecode, and how does it relate to Java's platform independence?What is bytecode, and how does it relate to Java's platform independence?Apr 27, 2025 am 12:06 AM

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!