下面列出了怎么用com.vaadin.server.FileDownloader的API类实例代码及写法,或者点击链接到github查看源代码。
private Button getArtifactDownloadButton(final Table table, final Object itemId) {
final String fileName = (String) table.getContainerDataSource().getItem(itemId)
.getItemProperty(PROVIDED_FILE_NAME).getValue();
final Button downloadIcon = SPUIComponentProvider.getButton(
fileName + "-" + UIComponentIdProvider.ARTIFACT_FILE_DOWNLOAD_ICON, "",
i18n.getMessage(UIMessageIdProvider.TOOLTIP_ARTIFACT_DOWNLOAD), ValoTheme.BUTTON_TINY + " " + "blueicon",
true, FontAwesome.DOWNLOAD, SPUIButtonStyleNoBorder.class);
downloadIcon.setData(itemId);
FileDownloader fileDownloader = new FileDownloader(createStreamResource((Long) itemId));
fileDownloader.extend(downloadIcon);
fileDownloader.setErrorHandler(new ErrorHandler() {
/**
* Error handler for file downloader
*/
private static final long serialVersionUID = 4030230501114422570L;
@Override
public void error(com.vaadin.server.ErrorEvent event) {
uINotification.displayValidationError(i18n.getMessage(UIMessageIdProvider.ARTIFACT_DOWNLOAD_FAILURE_MSG));
}
});
return downloadIcon;
}
protected void initializeDownload() {
//
// Create a stream resource pointing to the file
//
StreamResource r = new StreamResource(new StreamResource.StreamSource() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getStream() {
try {
return new FileInputStream(self.file);
} catch (Exception e) {
logger.error("Failed to open input stream " + self.file);
}
return null;
}
}, self.file.getName());
r.setCacheTime(-1);
r.setMIMEType("application/xml");
//
// Extend a downloader to attach to the Export Button
//
FileDownloader downloader = new FileDownloader(r);
downloader.extend(this.buttonExport);
}
private void constructBody() {
final MVerticalLayout layout = new MVerticalLayout().withFullWidth();
CssLayout iconWrapper = new CssLayout();
final ELabel iconEmbed = ELabel.fontIcon(FileAssetsUtil.getFileIconResource(content.getName()));
iconEmbed.addStyleName("icon-48px");
iconWrapper.addComponent(iconEmbed);
layout.with(iconWrapper).withAlign(iconWrapper, Alignment.MIDDLE_CENTER);
final GridFormLayoutHelper infoLayout = GridFormLayoutHelper.defaultFormLayoutHelper(LayoutType.ONE_COLUMN);
if (content.getDescription() != null) {
final Label descLbl = new Label();
if (!content.getDescription().equals("")) {
descLbl.setData(content.getDescription());
} else {
descLbl.setValue(" ");
descLbl.setContentMode(ContentMode.HTML);
}
infoLayout.addComponent(descLbl, UserUIContext.getMessage(GenericI18Enum.FORM_DESCRIPTION), 0, 0);
}
UserService userService = AppContextUtil.getSpringBean(UserService.class);
SimpleUser user = userService.findUserByUserNameInAccount(content.getCreatedUser(), AppUI.getAccountId());
if (user == null) {
infoLayout.addComponent(new UserLink(UserUIContext.getUsername(), UserUIContext.getUserAvatarId(),
UserUIContext.getUserDisplayName()), UserUIContext.getMessage(GenericI18Enum.OPT_CREATED_BY), 0, 1);
} else {
infoLayout.addComponent(new UserLink(user.getUsername(), user.getAvatarid(), user.getDisplayName()),
UserUIContext.getMessage(GenericI18Enum.OPT_CREATED_BY), 0, 1);
}
final Label size = new Label(FileUtils.getVolumeDisplay(content.getSize()));
infoLayout.addComponent(size, UserUIContext.getMessage(FileI18nEnum.OPT_SIZE), 0, 2);
ELabel dateCreate = new ELabel().prettyDateTime(DateTimeUtils.toLocalDateTime(content.getCreated()));
infoLayout.addComponent(dateCreate, UserUIContext.getMessage(GenericI18Enum.FORM_CREATED_TIME), 0, 3);
layout.addComponent(infoLayout.getLayout());
MButton downloadBtn = new MButton(UserUIContext.getMessage(GenericI18Enum.BUTTON_DOWNLOAD))
.withIcon(VaadinIcons.DOWNLOAD).withStyleName(WebThemes.BUTTON_ACTION);
List<Resource> resources = new ArrayList<>();
resources.add(content);
StreamResource downloadResource = StreamDownloadResourceUtil.getStreamResourceSupportExtDrive(resources);
FileDownloader fileDownloader = new FileDownloader(downloadResource);
fileDownloader.extend(downloadBtn);
MButton cancelBtn = new MButton(UserUIContext.getMessage(GenericI18Enum.BUTTON_CANCEL), clickEvent -> close())
.withStyleName(WebThemes.BUTTON_OPTION);
MHorizontalLayout buttonControls = new MHorizontalLayout(cancelBtn, downloadBtn);
layout.with(buttonControls).withAlign(buttonControls, Alignment.MIDDLE_RIGHT);
this.setContent(layout);
}
private void addAttachmentRow(Content attachment) {
String docName = attachment.getPath();
int lastIndex = docName.lastIndexOf("/");
if (lastIndex != -1) {
docName = docName.substring(lastIndex + 1);
}
final AbsoluteLayout attachmentLayout = new AbsoluteLayout();
attachmentLayout.setWidth(WebUIConstants.DEFAULT_ATTACHMENT_THUMBNAIL_WIDTH);
attachmentLayout.setHeight(WebUIConstants.DEFAULT_ATTACHMENT_THUMBNAIL_HEIGHT);
attachmentLayout.setStyleName("attachment-block");
MCssLayout thumbnailWrap = new MCssLayout().withFullSize().withStyleName("thumbnail-wrap");
Link thumbnail = new Link();
thumbnail.setTargetName("_blank");
if (StringUtils.isBlank(attachment.getThumbnail())) {
thumbnail.setIcon(FileAssetsUtil.getFileIconResource(attachment.getName()));
} else {
thumbnail.setIcon(VaadinResourceFactory.getResource(attachment.getThumbnail()));
}
if (MimeTypesUtil.isImageType(docName)) {
thumbnail.setResource(VaadinResourceFactory.getResource(attachment.getPath()));
}
Div contentTooltip = new Div().appendChild(new Span().appendText(docName).setStyle("font-weight:bold"));
Ul ul = new Ul().appendChild(new Li().appendText(UserUIContext.getMessage(FileI18nEnum.OPT_SIZE_VALUE,
FileUtils.getVolumeDisplay(attachment.getSize())))).setStyle("line-height:1.5em");
ul.appendChild(new Li().appendText(UserUIContext.getMessage(GenericI18Enum.OPT_LAST_MODIFIED,
UserUIContext.formatPrettyTime(DateTimeUtils.toLocalDateTime(attachment.getLastModified())))));
contentTooltip.appendChild(ul);
thumbnail.setDescription(contentTooltip.write(), ContentMode.HTML);
thumbnail.setWidth(WebUIConstants.DEFAULT_ATTACHMENT_THUMBNAIL_WIDTH);
thumbnailWrap.addComponent(thumbnail);
attachmentLayout.addComponent(thumbnailWrap, "top: 0px; left: 0px; bottom: 0px; right: 0px; z-index: 0;");
MCssLayout attachmentNameWrap = new MCssLayout().withWidth(WebUIConstants.DEFAULT_ATTACHMENT_THUMBNAIL_WIDTH)
.withStyleName("attachment-name-wrap");
Label attachmentName = new ELabel(docName).withStyleName(WebThemes.TEXT_ELLIPSIS);
attachmentNameWrap.addComponent(attachmentName);
attachmentLayout.addComponent(attachmentNameWrap, "bottom: 0px; left: 0px; right: 0px; z-index: 1;");
MButton trashBtn = new MButton("", clickEvent -> {
ConfirmDialogExt.show(UI.getCurrent(), UserUIContext.getMessage(GenericI18Enum.DIALOG_DELETE_TITLE,
AppUI.getSiteName()), UserUIContext.getMessage(GenericI18Enum.CONFIRM_DELETE_ATTACHMENT),
UserUIContext.getMessage(GenericI18Enum.ACTION_YES),
UserUIContext.getMessage(GenericI18Enum.ACTION_NO),
confirmDialog -> {
if (confirmDialog.isConfirmed()) {
resourceService.removeResource(attachment.getPath(), UserUIContext.getUsername(), true, AppUI.getAccountId());
((ComponentContainer) attachmentLayout.getParent()).removeComponent(attachmentLayout);
}
});
}).withIcon(VaadinIcons.TRASH).withStyleName("attachment-control");
attachmentLayout.addComponent(trashBtn, "top: 9px; left: 9px; z-index: 1;");
MButton downloadBtn = new MButton().withIcon(VaadinIcons.DOWNLOAD).withStyleName("attachment-control");
FileDownloader fileDownloader = new FileDownloader(VaadinResourceFactory.getInstance().getStreamResource(attachment.getPath()));
fileDownloader.extend(downloadBtn);
attachmentLayout.addComponent(downloadBtn, "right: 9px; top: 9px; z-index: 1;");
this.addComponent(attachmentLayout);
}