【问题标题】:Download file vaadin下载文件 vaadin
【发布时间】:2013-06-29 02:11:51
【问题描述】:

我创建了一个表,它的数据源设置为 BeanItemContainer。每个 bean 都有一个名称(字符串)和一个 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;
        }


    });

所以getFile从bean中获取字节数组

【问题讨论】:

    标签: java file web-applications bytearray vaadin


    【解决方案1】:

    如果您使用的是 Vaadin 7,则可以使用 FileDownloader 扩展名,如下所述:https://vaadin.com/forum#!/thread/2864064

    您需要扩展按钮,而不是使用 clicklistener:

    Button l = new Button("Link to pdf");
    StreamResource sr = getPDFStream();
    FileDownloader fileDownloader = new FileDownloader(sr);
    fileDownloader.extend(l);
    

    获取 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;
    }
    

    【讨论】:

    • 注意:在初始化文件下载器之前文件必须存在。
    • 正如@SüniÚr 提到的,我们在初始化文件下载器之前需要该文件,但该文件可以是任何文件,您可以稍后通过说 fileDownloader.setFileDownloadResource(newResource) 来更改资源(如果有人需要这个)
    【解决方案2】:

    Book of Vaadin 中很好地描述了生成列的创建,对代码的一个更正是检查 columnId 或 propertyId 以确保您在右列中创建一个按钮 - 目前似乎您为任何列返回一个按钮。

    类似这样的:

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

    下载文件:

    
    
        // 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);
    
    

    关于如何处理资源的更多信息可以在Book of Vaadin找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2014-10-15
      • 2023-01-04
      • 2022-01-01
      • 2020-09-29
      相关资源
      最近更新 更多