java - 为什么 Upload.setAcceptMimeTypes 不能工作两次?
问题描述
提前抱歉,我的英语不太好。为什么,当我再次尝试上传文件时,我的 MIME 过滤器(由 setAcceptMimeTypes 安装)消失了,我可以再次看到所有文件?
第一次上传:
第二次上传:
我的代码:
@SpringUI
public class MyUI extends UI{
@Value("${app.path}")
private String basePath;
@Value("${app.mimeTypes}")
private String mimeType;
@Value("${app.maxSize}")
private int maxSize;
private double fileSize;
private File file;
Upload upload;
private OutputStream os = null;
@Override
protected void init(VaadinRequest request) {
final VerticalLayout baseLayout = new VerticalLayout();
TabSheet sample = new TabSheet();
sample.setHeight(100.0f, Unit.PERCENTAGE);
sample.addStyleName(ValoTheme.TABSHEET_FRAMED);
sample.addStyleName(ValoTheme.TABSHEET_PADDED_TABBAR);
HorizontalLayout uploadLayout = new HorizontalLayout();
addUploadLayout(uploadLayout);
HorizontalLayout downloadLayout = new HorizontalLayout();
addDownloadLayout(downloadLayout);
sample.addTab(uploadLayout, "Upload");
sample.addTab(downloadLayout, "Download");
baseLayout.addComponent(sample);
setContent(baseLayout);
}
private void addUploadLayout(HorizontalLayout layout){
upload = new Upload();
upload.setImmediateMode(false);
upload.setButtonCaption("Upload File");
upload.setAcceptMimeTypes(mimeType);
upload.setReceiver((Upload.Receiver) (filename, mimeType) -> {
file = new File(basePath + filename);
try {
os = new FileOutputStream(file);
return os;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
});
upload.addProgressListener((readBytes, contentLength) -> {
fileSize = (double) contentLength / 1024;
if (fileSize > maxSize){
upload.interruptUpload();
Notification.show("Слишком большой файл.", Notification.Type.ERROR_MESSAGE);}
});
upload.addSucceededListener(event -> {
DecimalFormat df = new DecimalFormat("0.00");
Notification.show("Загрузка завершена, загружено " + df.format(fileSize) + " кб.", Notification.Type.TRAY_NOTIFICATION);
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
});
upload.addFailedListener(event -> {
file.delete();
Notification.show("Ошибка загрузки.", Notification.Type.TRAY_NOTIFICATION);
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
});
layout.setMargin(true);
layout.addComponent(upload);
}
private void addDownloadLayout(HorizontalLayout layout){
Label label = new Label("Hello People!!!");
layout.setMargin(true);
layout.addComponent(label);
}
}
解决方案
推荐阅读
- python - 如何识别 0 和 1 网格中的等腰三角形?
- php - DOM.pdf 文件写入服务器但文件本身为空
- node.js - 将大量消息峰值传递给 AWS SQS 的最佳解决方案
- mysql - 如何优化包含相同“in”语句的sql?
- java - 如何在运行微服务时禁用/启用 Micrometer 指标
- python - 如何访问字典中的字典对象(带有for循环)?蟒蛇 3.6
- c# - 使用 oracle 和 C# 比较两个选择的结果
- java - solr wordbreaksolrspellchecker 中的 ArrayIndexOutofBoundsException 获取建议
- java - 我想实现一个通用数据模型类,它有一个字符串类型的属性
- firebase - flutter 从 Firebase 获取插入的 id