Upload without storing in memory? Issues with PipedOutputStream/PipedInputStream

I am basically trying to “forward” the uploaded file to another device, thus storing it on the vaadin webserver or in its ram, is not really needed, or wanted since the files can get pretty huge.

                        Upload upload = new Upload((MultiFileReceiver) new MultiFileReceiver() {
                        @Override
                        public OutputStream receiveUpload(String fileName, String mimeType) {
                            PipedOutputStream out = new PipedOutputStream();
                            try {
                                PipedInputStream in = new PipedInputStream(out);
                                Exec.later(ui1, task -> {
                                    try {
                                        // This reads from in and forwards/writes to another device via a regular Java socket
                                        //conFileManager.forwardFile(currentDir, fileName, in);
                                    } catch (Exception e) {
                                        //...
                                    } finally {
                                        in.close();
                                        out.close();
                                    }
                                });
                            } catch (IOException e) {
                                //...
                            }
                            return out;
                        }
                    });

Now to the really weird part(s): The forwarded file is 10x the original size once received, and the code above seems to work fine exactly like that with the only difference being that its executed inside a listener and using the memory buffer, like shown here:

                    MultiFileMemoryBuffer buffer = new MultiFileMemoryBuffer();
                    Upload upload = new Upload(buffer);
                    upload.addSucceededListener(event -> {...});

Sounds weird indeed. I can’t tell anything specific from your code sample. If you can create a standalone sample project, I’d recommend filing an issue at Issues · vaadin/flow · GitHub.

Pro tip: UploadFileHandler from in.virit:viritin.

The example is bit overwhelming, but you essentially get an input stream of your uploaded file content and then do what you want with it. No temporary files or buffers.

Can confirm that the viritin UploadFileHandler is way more intuitive than using PipedInputStream.

1 Like