2013-03-29 37 views
0

我的應用程序(帶有Java EE的Wicket框架)具有類似嚮導的樣式,並生成一個xml文件。我想爲用戶提供一個按鈕,用於下載文件。我如何提供這樣的功能,最好不要將文件保存在服務器上?允許用戶在Java EE/Wicket上下載生成的XML

任何幫助將非常感激

回答

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,如上例所示。

1

我只是用一個org.apache.wicket.markup.html.link.DownloadLink

HTML:

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

的Java:

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

我將我的文件存儲在服務器上r,但這裏有更多關於如何在飛行中生成文件的更多信息的問題: How to use Wicket's DownloadLink with a file generated on the fly?