春ブーツにファイルを送信します

ユーセフelhayani:

私は、私はこのコードを使用これに、身体の応答にファイルを送信春のブートアプリケーションを作成しています:

FileSystemResource pdfFile = new FileSystemResource(outputFile);

return ResponseEntity
       .ok()
       .contentLength(pdfFile.contentLength())
       .contentType(MediaType.parseMediaType("application/pdf"))
       .body(new ByteArrayResource(IOUtils.toByteArray(pdfFile.getInputStream())));

FileSystemResourceを使用するよりも、他の送信ファイルの任意の別の方法があります場合、私は思ったんだけど?

、どんな提案がある場合、躊躇しないでください。

ありがとうございました !

DarkSmurf:

これは、私は通常それを行う方法の簡略化されたバージョンですが、それはかなり同じことを行います。

@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public ResponseEntity<byte[]> getPdf(@PathVariable Long id) throws IOException {
    final String filePath = pdfFilePathFinder.find(id);

    final byte[] pdfBytes = Files.readAllBytes(Paths.get(filePath));

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    headers.setContentDispositionFormData("attachment", null);
    headers.setCacheControl("no-cache");

    return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=214572&siteId=1