1
我试着在文件上传前比较这些文件。Vaadin上传组件如何在submitUpload之前获取fileName?
如果系统中存在名称存在的文件,请询问有关创建新版本或仅覆盖它的信息。
这里是问题,如何获取文件名?
我不能使用receiveUpload(),因为在这个方法文件被从上传组件中删除?
我试着在文件上传前比较这些文件。Vaadin上传组件如何在submitUpload之前获取fileName?
如果系统中存在名称存在的文件,请询问有关创建新版本或仅覆盖它的信息。
这里是问题,如何获取文件名?
我不能使用receiveUpload(),因为在这个方法文件被从上传组件中删除?
问题是,一旦您使用上传组件启动上载,只能通过调用interruptUpload()方法来中断它,并且无法在以后重新开始。 中断是永久性的。
这意味着您不能在上传过程中暂停以查看系统中是否已有该文件。您必须一直上传文件。
考虑到这个缺点,如果你有文件,你可以在上传结束后检查你的系统。如果你有这个文件,你可以显示一个确认对话框,让你选择保留文件或覆盖文件。
以下是我在“系统”检查一个例子(我只保留文件名字符串列表),如果该文件已被上传:
public class RestrictingUpload extends Upload implements Upload.SucceededListener, Upload.Receiver {
private List<String> uploadedFilenames;
private ByteArrayOutputStream latestUploadedOutputStream;
public RestrictingUpload() {
setCaption("Upload");
setButtonCaption("Upload file");
addSucceededListener(this);
setReceiver(this);
uploadedFilenames = new ArrayList<String>();
}
@Override
public OutputStream receiveUpload(String filename, String mimeType) {
latestUploadedOutputStream = new ByteArrayOutputStream();
return latestUploadedOutputStream;
}
@Override
public void uploadSucceeded(SucceededEvent event) {
if (fileExistsInSystem(event.getFilename())) {
confirmOverwrite(event.getFilename());
} else {
uploadedFilenames.add(event.getFilename());
}
}
private void confirmOverwrite(final String filename) {
ConfirmDialog confirmDialog = new ConfirmDialog();
String message = String.format("The file %s already exists in the system. Overwrite?", filename);
confirmDialog.show(getUI(), "Overwrite?", message, "Overwrite", "Cancel", new ConfirmDialog.Listener() {
@Override
public void onClose(ConfirmDialog dialog) {
if (dialog.isConfirmed()) {
copyFileToSystem(filename);
}
}
});
}
private void copyFileToSystem(String filename) {
try {
IOUtils.write(latestUploadedOutputStream.toByteArray(), new FileOutputStream(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
private boolean fileExistsInSystem(String filename) {
return uploadedFilenames.contains(filename);
}
}
注我已用2个的外部库:
你可以得到这个类来自Gist代码片段:https://gist.github.com/gabrielruiu/9960772您可以粘贴到你的用户界面,并对其进行测试。
尝试查看此[限制文件类型](http://stackoverflow.com/questions/21913526/restricting-file-types-upload-component) – Skizzo