允许用户在Java EE / Wicket上下载生成的XML

时间:2013-03-29 13:24:10

标签: xml java-ee dynamic download wicket

我的应用程序(带有Java EE的Wicket框架)具有类似向导的样式并生成xml文件。我想为用户提供一个按钮,可以使用该按钮下载文件。我怎样才能提供这样的功能,最好不要将文件保存在服务器上?

任何帮助都会非常感激

2 个答案:

答案 0 :(得分:1)

我只使用org.apache.wicket.markup.html.link.DownloadLink

HTML:

<input wicket:id="export" type="button">

爪哇:

File file = new File(appHome + "/template.xlsx");
add(new DownloadLink("export", file, "template.xlsx"));`

我将我的文件存储在服务器上,但这是另一个问题,其中包含有关如何动态生成文件的更多信息: How to use Wicket's DownloadLink with a file generated on the fly?

答案 1 :(得分:1)

这个例子是开始的好点:AJAX update and file download in one blow

然后,您唯一需要做的就是为生成XML实现IResourceStream。

public class YourXmlDownload implements Serializable, IResourceStream {


    protected byte[] xmlContent = null;

    // ...

    @Override
    public final Bytes length() {
        return Bytes.bytes(xmlContent.length);
    }

    @Override
    public final InputStream getInputStream() throws ResourceStreamNotFoundException {
        return new ByteArrayInputStream(xmlContent);
    }

}

您可以使用ByteArrayInputStream,如上例所示。