我的应用程序(带有Java EE的Wicket框架)具有类似向导的样式并生成xml文件。我想为用户提供一个按钮,可以使用该按钮下载文件。我怎样才能提供这样的功能,最好不要将文件保存在服务器上?
任何帮助都会非常感激
答案 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,如上例所示。