简体   繁体   中英

Vaadin Upload component how get fileName before submitUpload?

I try make implementation for comparing the files before they are uploaded.

If file whith name is exist in system ask about create new version or just override it.

Here is the problem, how to get file name?

I can't use receiveUpload(), because after this method file is remove from upload component ?

The problem is that once you start an upload using the Upload component, it can only be interrupted by calling the interruptUpload() method, and you cannot resume anytime later. The interruption is permanent.

This means you cannot pause in the middle of the upload to see if you already have the file in your system. You have to upload the file all the way.

Considering this drawback, you can sill check in your system if you have the file, after the upload finishes. If you have the file, you can show a confirmation dialog in which you decide wether to keep the file or overwrite.

The following is an example in which I check in the "system" (I just keep a String list with the filenames) if the file has already been uploaded:

public class RestrictingUpload extends Upload implements Upload.SucceededListener, Upload.Receiver {

private List<String> uploadedFilenames;
private ByteArrayOutputStream latestUploadedOutputStream;

public RestrictingUpload() {
    setCaption("Upload");
    setButtonCaption("Upload file");
    addSucceededListener(this);
    setReceiver(this);
    uploadedFilenames = new ArrayList<String>();
}

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
    latestUploadedOutputStream = new ByteArrayOutputStream();
    return latestUploadedOutputStream;
}

@Override
public void uploadSucceeded(SucceededEvent event) {
    if (fileExistsInSystem(event.getFilename())) {
        confirmOverwrite(event.getFilename());
    } else {
        uploadedFilenames.add(event.getFilename());
    }
}

private void confirmOverwrite(final String filename) {
    ConfirmDialog confirmDialog = new ConfirmDialog();
    String message = String.format("The file %s already exists in the system. Overwrite?", filename);
    confirmDialog.show(getUI(), "Overwrite?", message, "Overwrite", "Cancel", new ConfirmDialog.Listener() {
        @Override
        public void onClose(ConfirmDialog dialog) {
            if (dialog.isConfirmed()) {
                copyFileToSystem(filename);
            }
        }
    });
}

private void copyFileToSystem(String filename) {
    try {
        IOUtils.write(latestUploadedOutputStream.toByteArray(), new FileOutputStream(filename));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }
}

private boolean fileExistsInSystem(String filename) {
    return uploadedFilenames.contains(filename);
}

}

Note that I have used 2 external libraries:

You can get the code snippet for this class from Gist: https://gist.github.com/gabrielruiu/9960772 which you can paste into your UI and test it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM