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.AuthenticatorBase.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?