Vaadin 6上传组件:如何终止文件上传进程而不在服务器上保存错误文件

时间:2016-04-28 18:47:02

标签: java vaadin vaadin6

在我的应用程序中,我有一个上传按钮。 当用户选择某个文件时,我必须验证其扩展名。 (让用户只上传.txt文件)。如果它是txt - 将其保存在服务器上,否则 - 对此类文件不执行任何操作。

我尝试过这样的事情:

//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);
}

它几乎可以工作,除了事实,上传组件捕获错误:

  

com.vaadin.terminal.gwt.server.UploadException:上传失败   com.vaadin.terminal.gwt.server.AbstractCommunicationManager.streamToReceiver(AbstractCommunicationManager.java:623)     在   com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleSimpleMultipartFileUpload(AbstractCommunicationManager.java:476)     在   com.vaadin.terminal.gwt.server.CommunicationManager.handleFileUpload(CommunicationManager.java:259)     在   com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:495)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:723)at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)     在   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)     在   org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:620)     在   org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)     在java.lang.Thread.run(未知来源)引起:   com.vaadin.terminal.gwt.server.NoOutputStreamException at   com.vaadin.terminal.gwt.server.AbstractCommunicationManager.streamToReceiver(AbstractCommunicationManager.java:568)     ......还有17个

那么,用错误的文件终止上传的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

返回NullOutputStreamextends OutputStream)而不是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(); 
}

NullOutputStream类通过“package com.vaadin.external.org.apache.commons.io.output”打包,因此您应该可以使用该类。

如果您有任何疑问,请与我们联系!