简体   繁体   中英

Vaadin 6 Upload component: how to terminate file upload proccess without saving wrong file on a server

In my app i have an Upload button. When a user selects some file, i have to validate its extention. (Let user upload only .txt files). And if it is txt - save it on server, otherwise - do nothing with such file.

I've tried something like that:

//Check file type on start event:
@Override
public void uploadStarted(StartedEvent event) {
    String contentType = event.getFilename();
    if (!contentType.endsWith("txt")) {
        isValid = false;
        upload.interruptUpload();
    }
}

//If it is txt, do some actions:
@Override
public OutputStream receiveUpload(String filename, String mimeType) {
    FileOutputStream fos = null;
    if(isValid) {
        try {
            createUserDir(filename);
            fos = new FileOutputStream(file);
            //some more actions
        } catch (final java.io.FileNotFoundException e) {
            log.error(e);
            return null;
        }
    }
    return fos;
}

//Error msg for a user
@Override
public void uploadFailed(Upload.FailedEvent event) {
    root.getWindow().showNotification("Error Header", "Error Msg",
            Window.Notification.TYPE_ERROR_MESSAGE);
}

It nearly works, with the exception of the fact, upload component catches an error:

com.vaadin.terminal.gwt.server.UploadException: Upload failed at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.streamToReceiver(AbstractCommunicationManager.java:623) at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleSimpleMultipartFileUpload(AbstractCommunicationManager.java:476) at com.vaadin.terminal.gwt.server.CommunicationManager.handleFileUpload(CommunicationManager.java:259) at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:495) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.authenticator.Authe nticatorBase.invoke(AuthenticatorBase.java:563) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) Caused by: com.vaadin.terminal.gwt.server.NoOutputStreamException at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.streamToReceiver(AbstractCommunicationManager.java:568) ... 17 more

So, what is the right way to terminate Upload with a wrong file?

Return a NullOutputStream (which extends OutputStream ) instead of null:

//Check file type on start event:
@Override
public void uploadStarted(StartedEvent event) {
    String contentType = event.getFilename();
    if (!contentType.endsWith("txt")) {
        isValid = false;
        upload.interruptUpload();    
    }
}

//If it is txt, do some actions:
@Override
public OutputStream receiveUpload(String filename, String mimeType) {
    FileOutputStream fos = null; // Stream to write to
    if (filename != null && !filename.isEmpty() && isValid) {
        try {
            createUserDir(filename);
            fos = new FileOutputStream(file);
            return fos;
        } catch (final java.io.FileNotFoundException e) {
            log.error(e);
            return new NullOutputStream();
        }
    } else {/*Show error "Please select a file to upload."*/};

    uploadControl.interruptUpload();
    return new NullOutputStream(); 
}

The NullOutputStream class is packaged via "package com.vaadin.external.org.apache.commons.io.output" so you should have the class available to you.

Please let me know if you have any questions!

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