TextInsertController
package com.xiangbo.demo.controller;
import com.xiangbo.demo.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@Component
@RequestMapping
public class TextInsertController {
@Autowired
private HttpServletRequest request;
@GetMapping
public String index(Model model){
return "/index";
}
@RequestMapping("/insert/code")
@ResponseBody
public Object doCtrlService() throws Exception {
//获取前端传过来的代码数据
String classIdStr = request.getParameter("code");
String userId = "5657";
String ckpointId = "8";
/**
* 获取时间戳,作为文件名
*/
Calendar rightNow=Calendar.getInstance();
Date d1=rightNow.getTime();
SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMddHHmmss");
String nowtime=(sdf1.format(d1)).toString();
String str = "D:/python/code/"+userId+"/"+ckpointId+"/py/";
File dir=new File(str);
dir.mkdirs();
String makeBoatsLogStr=str+nowtime+"code.py";
/**
* 创建文件
*/
File makeBoatsLog=new File(makeBoatsLogStr);
//判断文件是否存在,若存在,先删除在重新创建
if (makeBoatsLog.exists()){
makeBoatsLog.delete();
makeBoatsLog=new File(makeBoatsLogStr);
}
FileWriter fw = new FileWriter(makeBoatsLog,true);
String lineEnd="\r\n";
fw.write(classIdStr);
fw.flush();
fw.close();
String s = FileUtils.runCommand("python " + makeBoatsLogStr);
System.out.println(s);
return s;
}
}
FileUtils
package com.xiangbo.demo.utils;
import java.io.*;
@SuppressWarnings("all")
public class FileUtils {
public static void main(String[] strings)
{
System.out.println(runCommand("ipconfig"));
}
/*方法一*/
public static String execCommand(String command)
{
String line = "";
StringBuilder sb = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(
Runtime.getRuntime().exec(command).getInputStream(),"gbk")))
{
while ((line = bufferedReader.readLine()) != null)
sb.append(line + "\r\n");
} catch (IOException e) { return "Invalid command."; }
return sb.toString();
}
/*方法二*/
public static String runCommand(String command){
InputStreamReader stdISR = null;
InputStreamReader errISR = null;
Process process = null;
String lines = null;
StringBuilder sb = new StringBuilder();
try {
process = Runtime.getRuntime().exec(command);
int exitValue = process.waitFor();
stdISR = new InputStreamReader(process.getInputStream(),"Shift-JIS");
BufferedReader stdBR = new BufferedReader(stdISR);
while ((lines = stdBR.readLine()) != null) {
sb.append(lines + "\r\n");
System.out.println("STD line:" + lines);
}
errISR = new InputStreamReader(process.getErrorStream(),"Shift-JIS");
BufferedReader errBR = new BufferedReader(errISR);
while ((lines = errBR.readLine()) != null) {
sb.append(lines + "\r\n");
System.out.println("ERR line:" + lines);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (stdISR != null) {
stdISR.close();
}
if (errISR != null) {
errISR.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
System.out.println("正式执行命令:" + command + "有IO异常");
}
}
return sb.toString();
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script th:src="@{/js/jquery-3.2.1.min.js}"></script>
</head>
<body>
<textarea id="code" class="form-control" placeholder="请输入代码....." style="width: 500px;height: 400px;float: left;"></textarea>
<textarea id="data" class="form-control" placeholder="终端输出显示:" style="width: 500px;height: 400px;"></textarea>
<div style="border: 1px solid;width: 100px;border-radius: 10px;
height: 30px;font-size: larger;text-align: center; background: aqua" id="submit">提交</div>
<script>
$(function () {
$("#submit").click(function () {
text();
}
);
})
function text() {
var code = $("#code").val();
$.ajax({
url:"insert/code",
type:"post",
data:{"code":code},
success:function (data) {
$("#data").val(data);
//alert(data);
}
});
}
</script>
</body>
</html>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>