【发布时间】:2019-09-01 20:23:21
【问题描述】:
我正在使用 Spring Boot 创建 Web 应用程序,在此 Web 应用程序中我需要使用 Runtime.getRuntime() 运行命令行,但问题是:此命令在 Web 应用程序之后执行 这不是我想做的,问题是:
"如何确保 Runtime.getRuntime() 指令启动 当它在 Spring Boot 应用程序中被调用时(不是在它的末尾)"
控制器:
@PostMapping("/toLinPDf")
public ResponseEntity<ByteArrayResource> convertion(@RequestParam(value = "input", required = false) String in,
@RequestParam(value = "output", required = false) String out) throws IOException, InterruptedException, ExecutionException {
// the methode LinearizePDF contain the command line
linearizeService.LinearizePDf(in, out);
logger.warn("call the method linearizeService.LinearizePDf ");
FileSystemResource result = new FileSystemResource(out);
return ResponseEntity
.ok()
.contentLength(result.contentLength())
.contentType(
MediaType.parseMediaType("application/pdf"))
.body(new ByteArrayResource(IOUtils.toByteArray(result.getInputStream())));
}
linearizeService(包含方法 LinearizePDf(in, out)):
@Async
public void LinearizePDf(String Input , String Output) throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
// the command line
String command = "qpdf --linearize " + Input + " " + Output;
Process pr = rt.exec(command );
logger.warn("run the command");
pr.destroy();
}
请,如果有任何建议,请不要犹豫。
谢谢!
【问题讨论】:
标签: java spring-boot command-line process