Upload Component FileBuffer

Vaadin version: 13.0.7
Hey everyone,

I am using the upload component. However when I try to access the inputstream of the FileBuffer or MultiFileBuffer it says stream closed. When I use the MemoryBuffer or it’s multi counterpart it works. I prefer to use FileBuffer because I don’t want to have the documents in memory of the application. Does anyone know what is causing this?

example code:
var:

private FileBuffer foodArticlesReceiver = new FileBuffer();

in the init method:

foodArticleUpload.setReceiver(foodArticlesReceiver);
foodArticleUpload.setMaxFileSize(10 * 1024 * 1024);
foodArticleUpload.setAcceptedFileTypes(allowedFileTypes.EXCELFILENAMEEXTENSIONS);
foodArticleUpload.setMaxFiles(1);
foodArticleUpload.addFinishedListener(e -> enableUploadButton(e,foodArticlesReceiver));

when the upload is finished:
note: This method is only used by me to test functionality. That’s why the names can be abit wacky.

private void enableUploadButton(FinishedEvent e, FileBuffer f){
try (InputStream inputStream =  f.getInputStream()){
 
		File targetFile = new File("src/main/resources/targetFile.xlxs");

		FileUtils.copyInputStreamToFile(inputStream, targetFile);
	} catch (IOException ex) {
            Logger.getLogger(ArticleUploadComponent.class.getName()).log(Level.SEVERE, null, ex);
	}
}

I see that it’s an issue:
https://github.com/vaadin/vaadin-upload-flow/issues/105

And there is the question - how to clean up tmp file after grabbing it to destination path?

We already have tmp file with our upload, and I want to simply move this file to new location.

Ok, I just created own buffer based on FileBuffer to get access to underlying tmp file. :slight_smile:

public class UploadBuffer extends FileBuffer {
    private File tmpFile;

    @Override
    protected FileOutputStream createFileOutputStream(String fileName) {
        try {
            tmpFile = createFile(fileName); // store reference
            return new FileOutputStream(tmpFile);
        } catch (IOException var3) {
            this.getLogger().log(Level.SEVERE, "Failed to create file output stream for: '" + fileName + "'", var3);
            return null;
        }
    }

    private File createFile(String fileName) throws IOException {
        String tempFileName = "upload_tmpfile_" + fileName + "_" + System.currentTimeMillis();
        return File.createTempFile(tempFileName, (String) null);
    }

    public File getTmpFile() {
        return tmpFile;
    }
}