Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

TUTORIALVaadin lets you build secure, UX-first PWAs entirely in Java.
Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Calling javascript synchronously by Enver Haase, 4 days ago
Edit Upload Document
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 {
...
}
Last updated on
You cannot reply to this thread.