亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

等到在 Java 中創建文件

等到在 Java 中創建文件

海綿寶寶撒 2022-09-28 15:42:42
我正在開發一個Web API(使用彈簧啟動),使用外部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尚未完成處理,為什么我得到這個錯誤,我的問題是:我該如何處理這個問題,我的意思是如何等待文件被創建然后發送這個文件?linearizeService.LinearizePDf(in, out);FileSystemResource pdfFile = new FileSystemResource(out);
查看完整描述

1 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

我建議你使用Java 8。Future API


這是您的資源的更新。


@PostMapping("/toLinPDf")

public ResponseEntity<ByteArrayResource> convertion(

    @RequestParam(value = "input", required = false) String in,

    @RequestParam(value = "output", required = false) String out) throws IOException, InterruptedException {

ExecutorService executorService = Executors.newSingleThreadExecutor();

Callable<String> callable = () -> {

        linearizeService.LinearizePDf(in, out);

        return "Task ended";

};

Future<String> future = executorService.submit(callable);

String result = future.get();

executorService.shutdown();

FileSystemResource pdfFile = new FileSystemResource(out);

return ResponseEntity

            .ok()

            .contentLength(pdfFile.contentLength())

            .contentType(

                    MediaType.parseMediaType("application/pdf"))

            .body(new ByteArrayResource(IOUtils.toByteArray(pdfFile.getInputStream())));


}


查看完整回答
反對 回復 2022-09-28
  • 1 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號