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

标签 java spring spring-boot asynchronous redirectstandardoutput

我正在开发一个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/

相关文章:

java - Android ListView 着色项目

java - 可以在 For 循环中初始化 boolean 数组吗?

java - 了解用于插入和更新语句的 jdbcTemplate

java - 使用 Java 8 和应用程序见解 SDK 版本 1.0.8 时,Azure Application Insights 不显示实时指标

spring - 在单元测试中覆盖 Autowiring 的 Bean

java - 使用 mysql 在 travis 上运行测试

java - 没有jsp的Spring安全

java - Spring AOP程序-实例化失败

java - 从单个主机注册 Spring Boot Eureka 客户端的多个实例

java - 如何在并行运行之前初始化 JUnit 测试类