Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to return a file from an Action?

Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 16, 2014

Hi,

I'd like to have WebWork action return a file. Here's my atlassian.xml:

<action name="export" class="com.playsql.requirement.RequirementAction" method="export">
                <result name="download" type="stream">inputStream</result>
            </action>

Here's my action:

public String export() {
        try {
            File file = File.createTempFile("export-", ".csv");
            FileUtils.writeStringToFile(file, "bleblebleleheldbelhebelbelbele");
            inputStream = new FileInputStream(file);
            return "download";
        } catch (IOException e) {
            return "error";
        }
    }

However, the browser just shows it, it doesn't download it and doesn't give it a name.

Using this question, I have tried:

<result name="download1" type="stream">
    <param name="contentType">text/html</param>
    <param name="inputName">inputStream</param>
    <param name="contentDisposition">filename=document.csv</param>
</result>

but I get this exception, which means WebWork doesn't detect that the contents of the <result> element isn't just text and it tries to parse it as if it were the location.

java.lang.NullPointerException
	at java.util.regex.Matcher.getTextLength(Matcher.java:1140)
	at java.util.regex.Matcher.reset(Matcher.java:291)
	at java.util.regex.Matcher.&lt;init&gt;(Matcher.java:211)
	at java.util.regex.Pattern.matcher(Pattern.java:888)
	at com.opensymphony.xwork.util.TextParseUtil.translateVariables(TextParseUtil.java:32)
	at com.opensymphony.webwork.dispatcher.WebWorkResultSupport.execute(WebWorkResultSupport.java:113)
	at com.opensymphony.xwork.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:263)

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 23, 2014

Note: The other example won't work in IE, because it won't give it the right name. Instead:

public class RequirementAction extends AbstractSpaceAction implements ServletResponseAware {
private HttpServletResponse response;
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

...
            response.setHeader("Content-Disposition", "attachment; filename=" + "\"requirement-export.csv\"");
            response.setHeader("Cache-Control", "max-age=0");
            response.setHeader("Pragma", "");
            response.setContentType("text/csv");

            OutputStream out = response.getOutputStream();
            IOUtils.copy(inputStream, out);
            out.close();

1 vote
Adrien Ragot 2
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 16, 2014

Atlassian Answers is an awesome rubber duck in the sense it helps the community too!

&lt;action name="export" class="com.playsql.requirement.RequirementAction" method="export"&gt;
    &lt;result name="download" type="stream"&gt;
        &lt;param name="contentType"&gt;text/csv&lt;/param&gt;
        &lt;param name="location"&gt;inputStream&lt;/param&gt; &lt;!-- Here's where I was mistaken --&gt;
        &lt;param name="contentDisposition"&gt;attachment; filename=requirement-export.csv&lt;/param&gt;
    &lt;/result&gt;
    &lt;result name="error" type="velocity"&gt;/error.vm&lt;/result&gt;
&lt;/action&gt;

0 votes
Karunesh Sarad November 25, 2016

Hi Adrein,

 

The content disposition header is not at all reflecting in user's downloaded file.. Did it work for you?

Here is my configuration:

&lt;result name="download" type="stream"&gt;
                    &lt;param name="contentType"&gt;application/vnd.openxmlformats-officedocument.wordprocessingml.document&lt;/param&gt;
                    &lt;param name="contentDisposition"&gt;attachment; filename=test.docx.html&lt;/param&gt;
                    &lt;param name="inputName"&gt;inputStream&lt;/param&gt;
                &lt;/result&gt;
TAGS
AUG Leaders

Atlassian Community Events