gpt4 book ai didi

java - 如何让用户使用 vaadin 文件下载器下载 zip 文件

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:50 25 4
gpt4 key购买 nike

我关注了这个 topic它完美地工作。这是为文件下载器创建资源的函数

 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");
}

但我不知道如何让它创建 zip 文件资源。我需要创建很多资源吗?谢谢

最佳答案

这是我自己想出的解决方案

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");
}

关于java - 如何让用户使用 vaadin 文件下载器下载 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com