package com.controller;
import org.springframework.web.bind.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
@GetMapping("/{id}")
public String findById(@PathVariable Long id) throws IOException, InterruptedException {
try {
// 调用CMD命令
String command = "ipconfig";
//String command = "ping";
Process process = Runtime.getRuntime().exec(command);
// 获取命令输出结果
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// 等待命令执行完成
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return "333";
}
}
ping命令