在vaadin中上传图像时出现NoOutputStreamException

时间:2014-08-09 12:09:31

标签: java vaadin image-uploading

尝试仅上传图片。问题是我不知道如何处理这种情况。

    @Override
    public OutputStream receiveUpload(String filename, String mimeType)  {
        FileOutputStream fos = null;

        if(mimeType.startsWith("image")) {
            picture = new Picture();
            picture.filename = filename;
            picture.mimeType = mimeType;

            picture = HibernateUtils.save(picture);
            product.pictures.add(picture);
            File dirs = new File(IMAGE_LOCATION);
            File file = new File(IMAGE_LOCATION + picture.id);
            if (!dirs.exists()) {
                dirs.mkdirs();
            }
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }

        } else {
            Notification.show("", Notification.Type.ERROR_MESSAGE);
        }
       return fos;
    }

如果我尝试上传不同类型的文件而不是fos,则问题是null变量将为image。问题是,我应该如何处理它?<​​/ p>

1 个答案:

答案 0 :(得分:2)

我做这样的事情:

...
} else {
    Notification.show("", Notification.Type.ERROR_MESSAGE);
    return new ByteArrayOutputStream();
}
...