There are two main ways to do it in the browser:
For modern browsers, use the HTML5 File API. See https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications
If you need to support old browser, you POST the file to a hidden iFrame. See http://viralpatel.net/blogs/ajax-style-file-uploading-using-hidden-iframe/
On the server side, I am going to assume you are using Spring Data REST (SDR) for your REST servlet. SDR doesn't do file uploads, but you can run SDR along with a regular Spring MVC Dispatcher Servlet in the same servlet.
The handler method in your MVC Dispatcher's FileController would look something like this, if saving to database:
@RequestMapping(value = "/files", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
List<FileEntity> upload(@RequestParam("file") MultipartFile[] multipartFiles) throws Exception {
List<FileEntity> response = new ArrayList<FileEntity>();
for (MultipartFile multipartFile : multipartFiles) {
FileEntity f = new FileEntity(multipartFile.getBytes(), multipartFile.getContentType(), multipartFile.getOriginalFilename());
fileRepository.save(f);
response.add(f);
}
return response;
}
You want SDR, Apache Commons FileUpload and IO on your classpath (or use the newer Servlet 3 API instead of Apache Commons). Add them as maven dependencies:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>