0

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.

Gourab27
  • 17
  • 5
  • check this https://stackoverflow.com/questions/29894154/chrome-neterr-incomplete-chunked-encoding-error and in which cases the error is throwed? – JRichardsz Dec 24 '19 at 13:23
  • I have checked the answer, and the issue is happening in local systems as well as when the application is deployed to our in house server. I have checked from other people's system also, issue occurs there too, but it is inconsistent, happening randomly. – Gourab27 Dec 24 '19 at 13:27

2 Answers2

0

I think your problem is the output.close().

I reviewed some samples and no body close the output. Spring should close it after the request.

Also I advice you, stress your app in order to detect unexpected behaviors.

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
0

I had this problem when spring-boot-starter-actuator:2.2.1.RELEASE dependency was added.

After a lot of search of what was miss-configured an upgrade of the entire project to spring-boot-starter-parent:2.3.5.RELEASE (that also upgraded to spring-boot-starter-actuator:2.3.5.RELEASE) fixed everything. With this upgrade the response was still chunked but properly interpreted by browser.

From these symptoms I think it was a bug in the way chunked response (http2 specific) was handled by spring. It was too hard to pinpoint the exact problem and we moved over with spring upgraded.

raisercostin
  • 8,777
  • 5
  • 67
  • 76