如何下载带有 p:fileDownload 的字符串?

How download a String with p:fileDownload?

String output = "qwerty123";

然后如何将该字符串导出到 StreamedContent 在 PrimeFaces 中?

您可以在此处查看示例:https://www.primefaces.org/showcase/ui/file/download.xhtml?jfwid=e2fc5

在 link 中,从 .jpg 导出的 StreamedContent 从网页目录中的资源中收集。 但就我而言,我只想将 output 上方的字符串转换为 .txt 然后将其流式传输到 StreamedContent 中而无需 Files.write();.

提前致谢。

您可以使用 ByteArrayInputStream 来流式传输字符串的内容:

DefaultStreamedContent.builder()
        .name("your.txt")
        .contentType("text/plain")
        .stream(() -> new ByteArrayInputStream("qwerty123".getBytes(StandardCharsets.UTF_8)))
        .build();

另请参阅:

  • How to convert Java String into byte[]?
  • Can we convert a byte array into an InputStream in Java?