The response body from restTemplate is truncated when the file is uploaded

I am using spring RestTemplate to upload a file. The file size is small. I want to get a base64 encoded string. but I see that the base64 encoded string is truncated from what it should be.

Here is my code

 RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add( new ByteArrayHttpMessageConverter()); StreamResourceReader reader = new StreamResourceReader(); restTemplate.execute(uri, HttpMethod.POST, null, new StreamResponseExtractor(reader)); return reader.getEncodedString(); 

StreamResourceReader.java

 public class StreamResourceReader { private String encodeString; public void read(InputStream content) { try { encodeString = Base64.encodeBase64String(IOUtils.toByteArray(content)); } catch (IOException e) { throw new IllegalStateException(e); } } public ByteArrayOutputStream getOutputStream(){ return outputStream; } public String getEncodedString() { return encodeString; } } 

StreamResponseExtractor.java

 public class StreamResponseExtractor implements ResponseExtractor<InputStream> { private StreamResourceReader reader; public StreamResponseExtractor(StreamResourceReader resourceReader) { this.reader=resourceReader; } @Override public InputStream extractData(ClientHttpResponse response) throws IOException { reader.read(response.getBody()); return null; } } 

EDIT just found out that inputStream is truncated. I do not know why and what correction. any help here would be appreciated.

thanks

0
source share
1 answer

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.

+1
source

Source: https://habr.com/ru/post/1241181/


All Articles