gpt4 book ai didi

java - Spring Boot中方法调用的标准输出重定向的另一种方式

转载 作者:行者123 更新时间:2023-12-02 05:33:27 32 4
gpt4 key购买 nike

我正在开发一个Spring Boot应用程序,其目的是接收用户的代码,编译,执行它并最终向用户展示他们的代码输出。为此,我创建了一个组件,负责执行代码并将输出保存在 txt 文件中(通过 PrintStream),如下所示:

@Component
public class SimpleExecutor implements Executor {
@Autowired
private FileStorageService fileStorageService;

public Path runClass(Class helloClass, String outputPath) throws Exception {
helloClass.getConstructor().newInstance();
Method method = helloClass.getMethod("main", String[].class);
try {
PrintStream fileStream = new PrintStream(outputPath);
System.setOut(fileStream);
String[] params = null;
method.invoke(null, (Object) params);
return Paths.get(outputPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.setOut(null);
}
return null;
}
}

上面的runClass方法由另一个@Async方法调用(因此它在应用程序之外的另一个线程中工作)。问题是,将 System.out 更改为我的 fileStream 意味着还要存储 Spring 调试信息(这当然不是有意的)。例如,我有时得到的不是简单的“Hello world”:

2019-05-17 01:43:58.371 DEBUG 9720 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : POST "/code/upload", parameters={}
2019-05-17 01:43:58.372 DEBUG 9720 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.springframework.http.ResponseEntity<java.lang.String> com.example.controllers.CompilerApiController.singleFileUpload(org.springframework.web.multipart.MultipartFile) throws java.io.UnsupportedEncodingException
2019-05-17 01:43:58.374 DEBUG 9720 --- [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-05-17 01:43:58.375 DEBUG 9720 --- [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["Successfully uploaded file!"]
2019-05-17 01:43:58.376 DEBUG 9720 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2019-05-17 01:43:59.163 DEBUG 9720 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : POST "/code/upload", parameters={}
2019-05-17 01:43:59.165 DEBUG 9720 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.springframework.http.ResponseEntity<java.lang.String> com.example.controllers.CompilerApiController.singleFileUpload(org.springframework.web.multipart.MultipartFile) throws java.io.UnsupportedEncodingException
2019-05-17 01:43:59.167 DEBUG 9720 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-05-17 01:43:59.167 DEBUG 9720 --- [nio-8080-exec-4] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["Successfully uploaded file!"]
2019-05-17 01:43:59.168 DEBUG 9720 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2019-05-17 01:44:00.101 DEBUG 9720 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : POST "/code/upload", parameters={}
2019-05-17 01:44:00.101 DEBUG 9720 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public org.springframework.http.ResponseEntity<java.lang.String> com.example.controllers.CompilerApiController.singleFileUpload(org.springframework.web.multipart.MultipartFile) throws java.io.UnsupportedEncodingException
2019-05-17 01:44:00.103 DEBUG 9720 --- [nio-8080-exec-5] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-05-17 01:44:00.103 DEBUG 9720 --- [nio-8080-exec-5] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing ["Successfully uploaded file!"]
2019-05-17 01:44:00.104 DEBUG 9720 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 200 OK
Hello world

还有其他方法可以重定向 method.invoke 调用的标准输出吗?这是打印我想要存储的内容的那个。

最佳答案

请尝试这样。

public Path runClass(Class helloClass, String outputPath) throws Exception {

........ other code

FileOutputStream f = new FileOutputStream("mySeparateLog.txt");
System.setOut(new PrintStream(f));

....... other code

}

关于java - Spring Boot中方法调用的标准输出重定向的另一种方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56188286/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com