【问题标题】:Vaadin Upload filestream no close?Vaadin 上传文件流没有关闭?
【发布时间】:2016-03-09 16:20:10
【问题描述】:

Vaadin 7.6.2

从这个文档,得到这个代码示例: https://vaadin.com/docs/-/part/framework/components/components-upload.html

// Show uploaded file in this placeholder
final Embedded image = new Embedded("Uploaded Image");
image.setVisible(false);

// Implement both receiver that saves upload in a file and
// listener for successful upload
class ImageUploader implements Receiver, SucceededListener {
    public File file;

    public OutputStream receiveUpload(String filename,
                                      String mimeType) {
        // Create upload stream
        FileOutputStream fos = null; // Stream to write to
        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;
        }
        return fos; // Return the output stream to write to
    }

    public void uploadSucceeded(SucceededEvent event) {
        // Show the uploaded file in the image viewer
        image.setVisible(true);
        image.setSource(new FileResource(file));
    }
};
ImageUploader receiver = new ImageUploader();

// Create the upload with a caption and set receiver later
Upload upload = new Upload("Upload Image Here", receiver);
upload.setButtonCaption("Start Upload");
upload.addSucceededListener(receiver);

// Put the components in a panel
Panel panel = new Panel("Cool Image Storage");
Layout panelContent = new VerticalLayout();
panelContent.addComponents(upload, image);
panel.setContent(panelContent);

对此我有几个问题:

  1. 为什么没有fos.close() 任何地方(在finally 块中)?
  2. 为什么会从方法返回OutputStream?尤其是当receiver 被传递给监听器时? Listener 是否正在关闭流?
  3. 在哪里关闭流?

【问题讨论】:

    标签: vaadin vaadin7


    【解决方案1】:

    如果您查看sources,您会看到:

    1. 为什么没有fos.close() 任何地方(在finally 块中)?

    因为关闭输出流不取决于您。该框架将处理读取它接收到的数据,将其写入文件并关闭流(即使出现问题)。这发生在 streamToReceiver() 方法中的 FileUploadHandler (line 529 in the current version)。

    1. 为什么从方法返回OutputStream?特别是当receiver 被传递到Listener 时? Listener 是否正在关闭流?

    因为Receiver 的唯一目的是告诉框架它应该将接收到的数据写入哪里。来自文档:

    /**
     * Interface that must be implemented by the upload receivers to provide the
     * Upload component an output stream to write the uploaded data.
     *
     * @author Vaadin Ltd.
     * @since 3.0
     */
    

    为了进一步阐明 receiver 被传递到 Listener 的困惑:事实并非如此。 ImageUploader 实现了两者

    因此,相同的ImageUploader 实例在Upload 组件的构造函数中传递,它将用作Receiver,并在addSucceededListener() 方法中传递,它将用作...你猜对了,SucceededListener。大多数人(可能?!)更喜欢在同一个类中实现所有这些接口,但如果您愿意,您可以选择在他们自己的类中实现每个功能,然后将适当的实例传递给每个方法。

    1. 在哪里关闭流?

    你没有,当上传完成时,框架会按照第 1 点的说明为你关闭它。与此同时,你可以去喝杯啤酒 :-)

    【讨论】:

      猜你喜欢
      • 2011-11-14
      • 2015-03-31
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2021-02-02
      • 2013-06-01
      相关资源
      最近更新 更多