To confirm that the input stream is indeed truncated, you can try a few things. What IOUtils.toByteArray(content) does is an internal input content buffer and returns a buffer. You can compare the length of the buffer array with the byte array that the file actually represents. You can do the latter using the code below
String filePath = "/test.txt"; byte[] fileByteArray= Files.readAllBytes(Paths.get(filePath));
Also ClientHttpResponse (client-side type of HTTP response) also has an input stream available for checking the content.
InputStream getBody() throws IOException;
As a test for this scenario, I created a spring boot Rest client using the Rest Template (using common code) and a service to download the file again using spring Boot. Comparing the base encoded string with the download and direct access to files, both return the same content (compared using the String equals method).
UPDATE . Another thing to try is to simply use java.net.HttpURLConnection in a simple program (see the help here ) and try loading the content and checking if it works correctly, because behind all the spring abstractions, in this case the underlying object used is HttpURLConnection only
SimpleClientHttpResponse extends AbstractClientHttpResponse { public InputStream getBody() throws IOException { InputStream errorStream = this.connection.getErrorStream(); this.responseStream = (errorStream != null ? errorStream : this.connection.getInputStream()); return this.responseStream; } ........... ........... }
If this also gives you the same problem, then it's time to look at the server. The server may not be sending complete data.
source share