在 vaadin 7 中,使用 FileDownloader
时如何延迟确定文件名?
final Button downloadButton = new Button("Download file");
FileDownloader downloader = new FileDownloader(new StreamResource(new StreamSource() {
@Override
public InputStream getStream () {
return new ByteArrayInputStream(expesiveCalculationOfContent());
}
}, "file.snub"));
downloader.extend(downloadButton);
在此代码示例中,清楚地显示文件名
- 垃圾
- 必须尽早了解。
如何惰性地确定下载文件的文件名?
最佳答案
我不知道它是否脏,但这可行:在调用其 super 方法之前扩展 FileDownloader.handleConnectorRequest() 来调用 StreamResource.setFilename() 。
{
final Button downloadButton = new Button("Download file");
final StreamResource stream = new StreamResource(
new StreamSource() {
@Override
public InputStream getStream() {
return new ByteArrayInputStream("Hola".getBytes());
}
}, "badname.txt");
FileDownloader downloader = new FileDownloader(stream) {
@Override
public boolean handleConnectorRequest(VaadinRequest request,
VaadinResponse response, String path)
throws IOException {
stream.setFilename("better-name.txt");
return super
.handleConnectorRequest(request, response, path);
}
};
downloader.extend(downloadButton);
layout.addComponent(downloadButton);
}
关于java - 使用 FileDownloader 时延迟确定文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815848/