【问题标题】:Allow user to download generated XML on Java EE/Wicket允许用户在 Java EE/Wicket 上下载生成的 XML
【发布时间】:2013-03-29 13:24:10
【问题描述】:

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

任何帮助将不胜感激

【问题讨论】:

    标签: xml jakarta-ee dynamic download wicket


    【解决方案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。

    【讨论】:

      【解决方案2】:

      我只使用 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"));`
      

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

      【讨论】:

        猜你喜欢
        • 2018-12-15
        • 1970-01-01
        • 2017-06-08
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多