[英]How to let user download zip file using vaadin file downloader
I've followed this topic and it perfectly works. 我已经按照这个主题进行了完美的工作。 Here's the function to create resource for file downloader
这是为文件下载器创建资源的功能
private StreamResource createResource() {
return new StreamResource(new StreamSource() {
@Override
public InputStream getStream() {
String text = "My image";
BufferedImage bi = new BufferedImage(100, 30, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawChars(text.toCharArray(), 0, text.length(), 10, 20);
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", bos);
return new ByteArrayInputStream(bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}, "myImage.png");
}
but i don't know how to make it create a resource of zip file. 但我不知道如何使它创建一个zip文件的资源。 Do i need to create many resources?.
我需要创建许多资源吗? Thank you
谢谢
Here's the solution that i figured out myself 这是我自己想出来的解决方案
private StreamResource createZipResource()
{
return new StreamResource(new StreamSource()
{
@Override
public InputStream getStream()
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try
{
ZipOutputStream out = new ZipOutputStream(byteArrayOutputStream);
for (int i = 0; i < listData.size(); i++)
{
if (listData.get(i).contains(".txt"))
{
out.putNextEntry(new ZipEntry(listData.get(i) + ".txt"));
}
else
{
out.write(listData.get(i).getBytes());
}
}
out.close();
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
catch (IOException e)
{
System.out.println("Problem writing ZIP file: " + e);
}
return null;
}
},"Filename.zip");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.