Search code examples
vaadinvaadin-flow

Vaadin Flow Upload Component - streaming upload?


I am uploading very large files, which exceed the available memory and thus I am using a FileBuffer as Receiver. Unfortunately, when uploading a large file, it takes very long to save it. Instead, I'd much rather start processing the file while it's still uploading.

So is there a way to receive the upload as a stream? Or could I implement my own Receiver, but then do I need to also implement my entire processing logic in that receiver?


Solution

  • Yes, you can implement your own Receiver; the built-in receivers like FileBuffer and MemoryBuffer are just helpers to cover basic use cases. The Receiver interface is simple:

    public interface Receiver extends Serializable {
    
        /**
         * Invoked when a new upload arrives.
         *
         * @param fileName
         *            the desired filename of the upload, usually as specified by
         *            the client
         * @param mimeType
         *            the MIME type of the uploaded file
         * @return stream to which the uploaded file should be written
         */
        OutputStream receiveUpload(String fileName, String mimeType);
    }
    

    Your task is to provide the OutputStream for the Upload component to write on; in the case of a FileBuffer, the class is creating a File object and a FileOutputStream. If you want to process the stream yourself, you can either extend FileOutputStream or create a custom OutputStream and implement the write method. A BufferedOutputStream is also a good option to investigate.