Benusko Benusko - 1 year ago 187
Java Question

How to download a xml file with content from inputtextarea?

I am trying to download a xml file using primefaces components. This part is working but I have on my page a inputtextarea, and I would like to have the text that I write in the inputtextarea written in the xml file that is downloaded. Could a developer help me ? Thank you.

my view :

<!DOCTYPE html>
<html xmlns=""

<title>File Download</title>
<p:dialog modal="true" widgetVar="statusDialog" header="Status" draggable="false" closable="false" resizable="false">
<p:graphicImage value="/images/loading11.gif" />

<p:inputTextarea id ="mytheinput" value="#{fileDownloadView.mytext}" cols="115" autoResize="true" rows="20" />

<p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-arrowthick-1-s">
<p:fileDownload value="#{fileDownloadView.file}" />

<script type="text/javascript">
function start() {

function stop() {


My bean :

public class FileDownloadView {

private StreamedContent file;
private String mytext;

public FileDownloadView() {
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(mytext);
file = new DefaultStreamedContent(stream, "xml", "yourfile.xml");

public StreamedContent getFile() {
return file;

public String getMytext() {
return mytext;


Answer Source

Few remarks

  1. Your p:inputTextarea should be inside the h:form element
  2. Your bean's mytext property must have a getter (ok) AND a setter (missing!)
  3. Your InputStream code comes from a PF example that returns the content of a resource picture file. You just want to create a stream from a string! Ask yourself How do I turn a String into a Stream in java?
  4. The InputStream must be created on the fly because of the changing text (i.e. inside getFile instead of constructor)

A little help

public StreamedContent getFile() {
    InputStream stream = new ByteArrayInputStream( mytext.getBytes() );
    StreamedContent file = new DefaultStreamedContent(stream, "xml", "yourfile.xml");
    return file;

public String getMytext() {
    return mytext;

public void setMytext(String mytext) {
    this.mytext = mytext;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download