Jsch+Hutool实现文件上传与远程命令执行

本文地址:Jsch+Hutool实现文件上传与远程命令执行

环境信息:
JDK版本:JDK_1.8
Jsch版本:0.1.5
Hutool版本:5.7.21


1、添加maven依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>

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

2、文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* Copyright © 2022 Yuech and/or its affiliates. All rights reserved. */
package com.yc.apollo.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ftp.AbstractFtp;
import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import com.jcraft.jsch.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* @author Yuech
* @version 1.0
* @since 2022-01-10 11:30
*/
public class FileUploadTest {

public static void main(String[] args) {

//本地文件路径
String localFile = "d:\\user\\yuechang\\desktop\\test.txt";
//上传到远程的文件路径,要保证登录用户有写权限
String remoteFile = "/app/deploy";
Session session = null;
Sftp sftp = null;
try {
session = JschUtil.openSession(HostInfoConstants.REMOTE_HOST,
HostInfoConstants.REMOTE_PORT,
HostInfoConstants.USERNAME,
HostInfoConstants.PASSWORD);

sftp = new Sftp(session, AbstractFtp.DEFAULT_CHARSET);
String normalize = FileUtil.normalize(remoteFile);
sftp.upload(normalize, new File(localFile));

} finally {
IoUtil.close(sftp);
}
System.out.println("上传成功");
}
}

3、远程命令执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Copyright © 2022 Yuech and/or its affiliates. All rights reserved. */
package com.yc.apollo.test;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ssh.ChannelType;
import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
* 远程命令执行测试类
*
* @author Yuech
* @version 1.0
* @since 2022-04-13 9:36
*/
public class CmdExecTest {

public static final String cmd = "mv /app/deploy/test.txt /app/deploy/test_20220413.txt";

public static void main(String[] args) throws IOException, JSchException {

Charset charset = CharsetUtil.CHARSET_UTF_8;
Session session = JschUtil.createSession(HostInfoConstants.REMOTE_HOST,
HostInfoConstants.REMOTE_PORT,
HostInfoConstants.USERNAME,
HostInfoConstants.PASSWORD);

ChannelExec channel = (ChannelExec) JschUtil.createChannel(session, ChannelType.EXEC);

// 添加环境变量
channel.setCommand(cmd);
InputStream inputStream = channel.getInputStream();
InputStream errStream = channel.getErrStream();
channel.connect();

final String[] error = new String[1];
final String[] result = new String[1];
//

try {
System.out.println(error[0] = IoUtil.read(errStream, charset));
} catch (Exception e) {
e.printStackTrace();
if (!StrUtil.contains(e.getMessage(), "Pipe closed")) {
// DefaultSystemLog.getLog().error("读取 exec err 流发生异常", e);
error[0] = "读取 exec err 流发生异常" + e.getMessage();
}
}

try {
result[0] = IoUtil.read(inputStream, charset);
} catch (Exception e) {
e.printStackTrace();
if (!StrUtil.contains(e.getMessage(), "Pipe closed")) {
//DefaultSystemLog.getLog().error("读取 exec 流发生异常", e);
result[0] = "读取 exec 流发生异常" + e.getMessage();
}
}
System.out.println("结束 \n" + result[0] + " " + error[0]);
}
}

4、参考信息

jpom