feat - 添加执行 shell 命令的 web api
This commit is contained in:
parent
20e6c76870
commit
ee87f42882
@ -18,13 +18,19 @@ import org.flowable.engine.RuntimeService;
|
||||
import org.flowable.engine.history.HistoricProcessInstance;
|
||||
import org.flowable.engine.repository.Deployment;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@ -242,4 +248,53 @@ public class TestController {
|
||||
List<String> tenantIds = workflowManageService.sync().getTenantIds();
|
||||
return CommonResponse.success(tenantIds);
|
||||
}*/
|
||||
|
||||
@GetMapping("log")
|
||||
public String log(@RequestParam String cmdStr) throws Exception {
|
||||
if(!StringUtils.hasText(cmdStr)) {
|
||||
return "命令不能为空";
|
||||
}
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", cmdStr);
|
||||
Process process = pb.start();
|
||||
InputStream inputStream = process.getInputStream();
|
||||
InputStream errorStream = process.getErrorStream();
|
||||
|
||||
StringBuilder outputBuilder = new StringBuilder();
|
||||
// 读取命令输出的线程
|
||||
Thread outputReader = new Thread(() -> {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine())!= null) {
|
||||
outputBuilder.append(line).append("\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
outputReader.start();
|
||||
|
||||
// 读取命令错误输出的线程
|
||||
Thread errorReader = new Thread(() -> {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(errorStream))) {
|
||||
String line;
|
||||
while ((line = reader.readLine())!= null) {
|
||||
outputBuilder.append("Error: ").append(line).append("\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
errorReader.start();
|
||||
|
||||
// 等待命令执行完成
|
||||
outputReader.join();
|
||||
errorReader.join();
|
||||
|
||||
return outputBuilder.toString();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return "Error executing command.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user