Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - How to stream large multipart file uploads to database without storing on local file system [duplicate]

Tags:

Spring boot's default MultiPartResolver interface handles the uploading of multipart files by storing them on the local file system. Before the controller method is entered, the entire multipart file must finish uploading to the server.

We are storing all of our uploaded files directly to a database and our servers have a very small disk quota, so if a large file is uploaded, we are seeing an IOExeption - Disk quota exceeded.

Is there a way to get the stream directly from the client's incoming request before Spring's MultiPartResolver stores the file on the local filesystem so the we can stream directly to our db?

like image 975
McLovin Avatar asked Jun 16 '16 23:06

McLovin


People also ask

What is MultipartFile spring?

public interface MultipartFile extends InputStreamSource. A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired ...


1 Answers

You could use apache directly, as described here https://commons.apache.org/proper/commons-fileupload/streaming.html.

@Controller public class UploadController {      @RequestMapping("/upload")     public String upload(HttpServletRequest request) throws IOException, FileUploadException {          ServletFileUpload upload = new ServletFileUpload();          FileItemIterator iterator = upload.getItemIterator(request);         while (iterator.hasNext()) {             FileItemStream item = iterator.next();              if (!item.isFormField()) {                 InputStream inputStream = item.openStream();                 //...             }         }     } } 

Make sure to disable springs multipart resolving mechanism.

application.yml:

spring:    http:       multipart:          enabled: false 
like image 70
Bernhard Avatar answered Oct 20 '22 21:10

Bernhard