Upload - commit file to git

Hi, my code:

I want to push file into git (this file user choosing in vaadin window upload)

My code (below) works ok, but it is quite uncomfortable for me (comments below)…
[code]
public OutputStream receiveUpload(String filename, String MIMEType)
{
this.filename = filename;

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(filepath + File.separator + filename)); //exist any possibility to no saving file in filepath (only commit to git)
    } catch (Exception e) {                                                         //How to omit it, I don't want to save file in filepath...
        return null;
    }
                    
    return fos;
}

public void uploadSucceeded(Upload.SucceededEvent event)
{               
    try {
        commitToGit(filepath + File.separator + filename);  //this method read file from filepath. Exist any possibilty to transfer file from upload panel to here without saving this file in filepath ?
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        File file = new File(filepath + File.separator + filename);   //removing file from filepath, it is no comfortable for me
        if (file != null) {
            file.delete();
        }      
    }  
}

[/code]

Hello Areq!

If your method commitToGit(…) requires a path to an existing file as an input, I don’t see a way around to saving the uploaded file to filepath. However, if you can modify that method to accept the uploaded file in memory and have it decide what to do with the data, you could go with something like this:

private final ByteArrayOutputStream os;

public OutputStream receiveUpload(String filename, String MIMEType) {
    this.filename = filename;
    os = new ByteArrayOutputStream();
    return os;
}

public void uploadSucceeded(Upload.SucceededEvent event) {
    try {
        commitToGit(os, filename); // give the stream as a parameter instead of the actual filepath
        } catch (Exception e) {
            e.printStackTrace();
    }
}