0

我正在从 Vaadin 7 CookBook 中学习 Vaadin,在第 3 章中,作者展示了一个使用 StreamVariable 和 Html5File 的拖放上传器示例,代码如下:

public class DragAndDropUploader extends VerticalLayout {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static final String REPOSITORY = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath()+"/WEB-INF/vaadin-repo/";

    public DragAndDropUploader() {
        final Table table = new Table();
        table.setSizeFull();
        table.addContainerProperty("File name", String.class, null);
        table.addContainerProperty("Size", String.class, null);
        table.addContainerProperty("Progress", ProgressBar.class, null);

        DragAndDropWrapper dropTable = new DragAndDropWrapper(table);
        dropTable.setDropHandler(new DropHandler() {

            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            public void drop(DragAndDropEvent event) {
                WrapperTransferable transferred = (WrapperTransferable) event.getTransferable();
                Html5File[] files = transferred.getFiles();
                if (files != null) {
                    for (Html5File file : files) {
                        ProgressBar progressBar = new ProgressBar();
                        progressBar.setSizeFull();
                        UI.getCurrent().setPollInterval(100);
                        table.addItem(new Object[] { file.getFileName(), 
                                                     getSizeAsString(file.getFileSize()),
                                                     progressBar
                        }, null);

                        StreamVariable streamVariable = createStreamVariable(file, progressBar);

                        file.setStreamVariable(streamVariable);
                    }
                }
                else {
                    Notification.show("Usupported file type", Type.ERROR_MESSAGE);
                }
            }

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return AcceptAll.get();
            }

        }); 
        addComponent(dropTable);
        setSizeUndefined();
    }

    private StreamVariable createStreamVariable(final Html5File file, final ProgressBar progressBar) {
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        return new StreamVariable() {

            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            public void streamingStarted(StreamingStartEvent event) {                               
            }

            @Override
            public void streamingFinished(StreamingEndEvent event) {
                try {
                    FileOutputStream fos = new FileOutputStream(REPOSITORY+file.getFileName());                 
                    System.out.println(outputStream.size()+ ": "+outputStream.toString() + " - " +fos.toString());
                    outputStream.writeTo(fos);
                } catch (IOException e) {
                    Notification.show("Streaming finished failing, please, retry", Type.ERROR_MESSAGE);
                }
                progressBar.setValue(new Float(1.0));
            }

            @Override
            public void streamingFailed(StreamingErrorEvent event) {
                Notification.show("Streaming failed, please, try again", Type.ERROR_MESSAGE);
            }

            @Override
            public void onProgress(StreamingProgressEvent event) {
                progressBar.setValue((float) event.getBytesReceived() / file.getFileSize());
            }

            @Override
            public boolean listenProgress() {
                return true;
            }

            @Override
            public boolean isInterrupted() {
                return false;
            }

            @Override
            public OutputStream getOutputStream() {
                return outputStream;
            }
        };
    }

    private String getSizeAsString(long size) {
        String unit = "B";
        int kB, MB;
        if (size >= (kB = (2 << 10))) {
            size = size / kB;
            unit = "kB";
        }
        else if (size >= (MB = 2 << 20)) {
            size = size / MB;
        }
        return size + " " + unit;
    }
}

REPOSITORY 是一个 vaadin-repo 文件夹的路径WebContent/WEB-INF

我的问题是outputStream.writeTo(fos);文件应该写入服务器的实际位置:

    @Override
    public void streamingFinished(StreamingEndEvent event) {
        try {
            FileOutputStream fos = new FileOutputStream(REPOSITORY+file.getFileName());                 
            System.out.println(outputStream.size()+ ": "+outputStream.toString() + " - " +fos.toString());
            outputStream.writeTo(fos);
        } catch (IOException e) {
            Notification.show("Streaming finished failing, please, retry", Type.ERROR_MESSAGE);
        }
        progressBar.setValue(new Float(1.0));
    }

但事实并非如此。当我上传然后检查该vaadin-repo文件夹时,它仍然是空的......

我没有得到任何异常(没有 FileNotFoundException,没有 IOException),所以问题不在于。REPOSITORY 路径有一些空格(但我认为这不是问题(正如我所说我没有得到任何 FileNotFoundException),并且我之前已经实现了 Vaadin 的上传器(通过Upload.Receiver内部接口)。

问题出在哪里?

4

1 回答 1

0

您的代码似乎是正确的,并且类似的版本对我有用。唯一的问题似乎是您没有关闭流。确保同时关闭outputstreamFileOutputStream fos.

于 2015-09-02T06:28:58.110 回答