首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让用户使用vaadin文件下载器下载zip文件

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

Stack Overflow用户
提问于 2016-07-24 10:26:58
回答 1查看 992关注 0票数 4

我跟踪了这个主题,它完美地工作了。下面是为文件下载程序创建资源的函数

代码语言:javascript
复制
 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文件的资源。我需要创造很多资源吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-28 15:15:05

这是我自己想出的解决办法

代码语言:javascript
复制
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"); 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38551031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档