等待在Java中创建文件
我正在使用一个Web API(带有Spring Boover),它使用外部C++ API转换PDF,这个程序正在工作,但是当我想在正文响应中发送文件时,我得到这个错误:
{
"timestamp": "2019-04-10T09:56:01.696+0000",
"status": 500,
"error": "Internal Server Error",
"message": "file [D:\\[Phenix-Monitor]1.pdf] cannot be resolved in the file system for checking its content length",
"path": "/convert/toLinPDf"}
控制员:
@PostMapping("/toLinPDf")
public ResponseEntity<ByteArrayResource> convertion(@RequestParam(value = "input", required = false) String in,
@RequestParam(value = "output", required = false) String out) throws IOException, InterruptedException {
linearizeService.LinearizePDf(in, out);
FileSystemResource pdfFile = new FileSystemResource(out);
return ResponseEntity
.ok()
.contentLength(pdfFile.contentLength())
.contentType(
MediaType.parseMediaType("application/pdf"))
.body(new ByteArrayResource(IOUtils.toByteArray(pdfFile.getInputStream())));
}
我猜问题出在linearizeService.LinearizePDf(in, out);
中,因为在这个方法中,我使用的是外部进程,所以发生的情况是,当我试图用FileSystemResource pdfFile = new FileSystemResource(out);
打开文件时,线性化服务还没有完成处理,所以我得到这个错误,我的问题是:我如何处理这个问题,我的意思是如何等待文件被创建,然后发送这个文件
# 1 楼答案
我建议您使用Java 8的
Future API
这里是你的资源更新