java 下载文件 vaadin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17413236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download file vaadin
提问by abden003
I made a taable which has its data source set to a BeanItemContainer. Each bean has a name (String) and a byte[] which holds a file converted to a byte[]. I added a button to each row which is suppose to download the file by first converting it to a pdf. I am having trouble implementing the downloading part here is the code relating:
我制作了一个表,它的数据源设置为 BeanItemContainer。每个 bean 都有一个名称 (String) 和一个 byte[],其中包含一个转换为 byte[] 的文件。我向每一行添加了一个按钮,假设通过首先将其转换为 pdf 来下载文件。我在实现下载部分时遇到问题,这是相关的代码:
public Object generateCell(Table source, Object itemId,
Object columnId) {
// TODO Auto-generated method stub
final Beans p = (Beans) itemId;
Button l = new Button("Link to pdf");
l.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// TODO Auto-generated method stub
try {
FileOutputStream out = new FileOutputStream(p.getName() + ".pdf");
out.write(p.getFile());
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
l.setStyleName(Reindeer.BUTTON_LINK);
return l;
}
});
So getFile gets the byte array from the bean
所以 getFile 从 bean 中获取字节数组
回答by kris54321
If you are using Vaadin 7 you can use FileDownloaderextension as described here: https://vaadin.com/forum#!/thread/2864064
如果您使用的是 Vaadin 7,您可以使用FileDownloader扩展,如下所述:https: //vaadin.com/forum#!/thread/2864064
Instead of using a clicklistener you would need to extend the button instead:
您需要扩展按钮而不是使用点击侦听器:
Button l = new Button("Link to pdf");
StreamResource sr = getPDFStream();
FileDownloader fileDownloader = new FileDownloader(sr);
fileDownloader.extend(l);
To get the StreamResource:
获取流资源:
private StreamResource getPDFStream() {
StreamResource.StreamSource source = new StreamResource.StreamSource() {
public InputStream getStream() {
// return your file/bytearray as an InputStream
return input;
}
};
StreamResource resource = new StreamResource ( source, getFileName());
return resource;
}
回答by Sergey Makarov
Generated columns creation is well described in Book of Vaadin, one correction to your code would be to check columnId or propertyId to ensure you create a button in right column - currently it seems you return a button for any column.
生成的列创建在Book of Vaadin 中有很好的描述,对您的代码的一个更正是检查 columnId 或 propertyId 以确保您在右列中创建一个按钮 - 目前似乎您为任何列返回一个按钮。
Something like this:
像这样的东西:
public Object generateCell(CustomTable source, Object itemId, Object columnId)
{
if ("Link".equals(columnId))
{
// ...all other button init code is omitted...
return new Button("Download");
}
return null;
}
To download a file:
要下载文件:
// Get instance of the Application bound to this thread
final YourApplication app = getCurrentApplication();
// Generate file basing on your algorithm
final File pdfFile = generateFile(bytes);
// Create a resource
final FileResource res = new FileResource(pdfFile, app);
// Open a resource, you can also specify target explicitly -
// i.e. _blank, _top, etc... Below code will just try to open
// in same window which will just force browser to show download
// dialog to user
app.getMainWindow().open(res);
More info on how to deal with resources can be found in Book of Vaadin.
更多关于如何处理资源的信息可以在Book of Vaadin 中找到。