I have a Spring Boot application that is an API for a mobile/web app. One of the endpoints accepts a MultipartFile (an image or video) like this:
@RequestMapping(value = "/x/y/z", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> post(@RequestParam("file") MultipartFile file) {
...
}
When the method is called I get the file contents and save it somewhere. However I've noticed a bug - if I kill the network connection on the mobile app during the upload, I don't get any sort of IOException
on the API side, and I end up with a truncated byte array:
try {
InputSteam inputStream = file.getInputStream();
byte[] allBytes = IOUtils.toByteArray(inputStream);
inputStream.close();
} catch (Exception ex) {
//never get to this
}
So the question is: How can I ensure the stream was not interrupted?
Using Spring Boot:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Thanks in advance.