Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 下载文件vaadin_Java_File_Web Applications_Bytearray_Vaadin - Fatal编程技术网

Java 下载文件vaadin

Java 下载文件vaadin,java,file,web-applications,bytearray,vaadin,Java,File,Web Applications,Bytearray,Vaadin,我制作了一个taable,它的数据源设置为BeanItemContainer。每个bean都有一个名称(字符串)和一个字节[],其中包含一个转换为字节[]的文件。我在每一行添加了一个按钮,该按钮将首先将文件转换为pdf文件,从而下载该文件。我在实现下载部分时遇到问题,以下是相关代码: public Object generateCell(Table source, Object itemId, Object columnId) { // T

我制作了一个taable,它的数据源设置为BeanItemContainer。每个bean都有一个名称(字符串)和一个字节[],其中包含一个转换为字节[]的文件。我在每一行添加了一个按钮,该按钮将首先将文件转换为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中获取字节数组,生成的列创建在中有详细描述,对代码的一个更正是检查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);


有关如何处理资源的更多信息,请参见。

如果您使用的是Vaadin 7,则可以使用扩展,如下所述:

您需要扩展按钮,而不是使用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;
}

注意:文件必须在初始化filedownloader之前存在。正如@SüniÚr提到的,在初始化文件下载程序之前,我们需要该文件,但该文件可以是任何文件,如果需要,您可以稍后更改资源,方法是:filedownloader.setFileDownloaderResource(newResource)(如果有人需要)