I have a REST API build using Java 8 and Spring Boot. The API is used to download files. I was using Spring Boot's StreamingResponseBody
class to send the file as a stream of Data.
The full code snippet can be found below:
StreamingResponseBody resp = new StreamingResponseBody() {
@Override
public void writeTo(final OutputStream output) throws IOException {
try {
Files.copy(myFile.toPath(), output);
} finally {
output.close();
boolean check = myFile.delete();
log.info("File {} deletion status : {}", myFile.getAbsolutePath(), check);
}
}
};
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + myFile.getName());
headers.add("Content-Type", "application/octet-stream");
return new ResponseEntity<StreamingResponseBody>(resp, headers, HttpStatus.OK);
The API is giving correct response for some requests, but for few cases it throws ERR_INCOMPLETE_CHUNKED_ENCODING error. I tried looking into many places but was not able to fix this issue. I am trying to call the API from an Angular 8 Web App to download a file. The issue comes for all the major browsers available, I tested using IE 11, Chrome, Firefox, Edge.