Vaadin上传功能

时间:2017-12-27 18:16:32

标签: java file upload vaadin

我有兴趣使用一个按钮从用户的计算机上拍摄照片并将其上传到我的服务器上。

我设法解决了服务器上传部分,但是我在处理来自用户计算机的路径方面遇到了困难。 Vaadin上传并没有为我提供完整的路径,但我希望它是动态的。查看文档,他们使用了一些临时位置,但我不知道如何实现它。

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
    }

我希望当文件选择器关闭时,我会得到某种文件路径或处理程序,所以我可以把它放在我的服务器上。

1 个答案:

答案 0 :(得分:2)

filename参数中,您将拥有上传文件的名称。 但是文件的路径不会发送到服务器,这是Web应用程序/ Web浏览器的限制之一。

使用您使用的代码,您将在服务器上的tmp文件夹中获得上传文件的副本。

无法通过网络浏览器直接访问客户端计算机上的文件。