Code: Select all
File file = new File(filePath);
//Added permission for file path
file.setExecutable(false);
file.setReadable(true);
file.setWritable(true);
// Create StreamingResponseBody
StreamingResponseBody responseBody = outputStream -> {
// Read file content
Path path = Paths.get(filePath);
byte[] data = Files.readAllBytes(path);
try (InputStream inputStream = new ByteArrayInputStream(data)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new RuntimeException("Error streaming PDF", e);
} finally {
deleteUserPrintDir(filePath, file);
data=null;
path=null;
}
};
return ResponseEntity.ok().header("Content-Disposition",
"attachment; fileName=\"" + file.getName() + "\"")
.contentType(org.springframework.http.MediaType.APPLICATION_PDF)
.body(responseBody);