A change needs to be made in the example code in the Upload document:
https://vaadin.com/docs/-/part/framework/components/components-upload.html
try {
// Open the file for writing.
file = new File("/tmp/uploads/" + filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
new Notification("Could not open file<br/>", e.getMessage(), Notification.Type.ERROR_MESSAGE) .show(Page.getCurrent());
return null; // <--- BAD
}
Returning
null
throws the following exception
com.vaadin.server.NoOutputStreamException.
The fix is to instead return
NullOutputStream()
.
import org.apache.commons.io.output.NullOutputStream;
try {
// Open the file for writing.
file = new File("/tmp/uploads/" + filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
new Notification("Could not open file<br/>", e.getMessage(), Notification.Type.ERROR_MESSAGE) .show(Page.getCurrent());
return NullOutputStream(); // <--- GOOD
}
Furthermore, it might be useful to show an example of how the method might handle incorrect files:
if (!filename.matches("(?i).+\\.(jpg|gif|png)$")) {
Notification.show("Must be a .jpg, .gif or .png");
return NullOutputStream();
}
try {
...
} catch {
...
}