I am learning Vaadin from the Vaadin 7 CookBook, on chapter 3 the authors show an example of a drag and drop uploader using StreamVariable and Html5File, here is the code:
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 is a path to a vaadin-repo folder inside WebContent/WEB-INF
.
My problem is that outputStream.writeTo(fos);
where actually the file should be written to the server:
@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));
}
But it's not. When I make an upload and then check that vaadin-repo
folder, it remains empty...
I do not get any exceptions (no FileNotFoundException, no IOException), so the problem is not that. REPOSITORY path have some spaces (but I think that this is not the problem (as I said I don't get any FileNotFoundException), and I have implemented Vaadin's uploaders previously (via the Upload.Receiver inner interface)).
Where is the problem?
Your code seems to be correct and a similar version is working for me. The only problem seems to be that you're not closing the streams. Make sure you close both outputstream
and the FileOutputStream
fos
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.