views:

131

answers:

1

I have a web application written using freemarker, webwork and java. Now when user clicks on "getReport", java code returns the string variable (named "otchet") which contains the whole report in plain text and the following page is displayed:

simple.ftl:

<#if (otchet?exists)>
     ${otchet}   
<#else>
    <@ww.text name="report.none"/>
</#if>

It is working OK. However, I would like instead to offer user this report (contained in the variable "otchet") as a text/plain file download.

How can I solve this problem?

A: 

This is exactly what the StreamResult result type is for.

Example:

In your WebWork XML:

<result name="download" type="stream">
    <param name="contentDisposition">filename=report.txt</param>
    <param name="contentType">text/plain;charset=UTF-8</param>
    <param name="inputName">inputStream</param>
    <param name="bufferSize">1024</param>
</result>

In your action:

public InputStream getInputStream() {
    try {
        return new ByteArrayInputStream(getOtchet().getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException ex) {
        // Shouldn't happen with UTF-8.
        ex.printStackTrace();
    }
}

public String doDownload() {
    if (SUCCESS.equals(execute()) {
        return "download";
    }
    else {
        return ERROR;
    }
}
ZoogieZork