How to return a file from an Action?

This is to properly answer an older question, in case anyone else happens across it. The old question is closed, so I am unable to answer on it.


Yup, that’s from 2014 and it’s still an issue…5 and a half years later.


	at java.util.regex.Matcher.getTextLength(
	at java.util.regex.Matcher.reset(
	at java.util.regex.Matcher.<init>(
	at java.util.regex.Pattern.matcher(
	at com.opensymphony.xwork.util.TextParseUtil.translateVariables(
	at com.opensymphony.webwork.dispatcher.WebWorkResultSupport.execute(
	at com.opensymphony.xwork.DefaultActionInvocation.executeResult(

I was not happy with the explanation of nor the solution to it; so, I dug into it and found out what is actually causing this exception.

The <result> XML you are creating eventually turns into a com.opensymphony.xwork.Result object. In Confluence v6.15.8, this comes from the xwork- dependency.

The exact implementation is determined by the result element’s type attribute. In our case, this is “stream”, which ends up as a com.opensymphony.webwork.dispatcher.StreamResult object. In Confluence v6.15.8, this comes from the webwork-2.1.5-atlassin-3.jar dependency.

Atlassian’s StreamResult extends WebWorkResultSupport (in the same package). This abstract class adds on a few parameters, such as location and parse.

Hmm, that’s funny, because here is the chunk of code that triggers the NPE:

        if (this.parse) {
            OgnlValueStack stack = ActionContext.getContext().getValueStack();
            finalLocation = TextParseUtil.translateVariables(this.location, stack);

To be more specific, it’s on the 3rd line, inside the translateVariables call. We never set location, because according to xwork/struts, this parameter is not relevant to stream type results. The parse parameter determines whether or not the location parameter will be parsed for variables (which are in the stack and can come from your action, etc - it’s very handy if you actually need the location parameter).

Since the parse parameter defaults to true and we aren’t using a location parameter, the location is parsed. The location is null, eventually java.util.regex.Matcher attempts to grab it’s length, and we get the NPE.

To compare, I checked the current version in struts2 (webwork 2.3.x and beyond were merged into struts2). Their version does not extend WebWorkResultSupport. Nor should it, as location and parse are not relevant to stream types. It simply implements Result.

Why Atlassian extended this - we may never know… If they were to default parse to false in StreamResult or stopped extending WebWorkResultSupport, this would no longer be a problem.

Now that we know the problem, the fix is easy: disable parsing of the location. This can be done by adding the following to your <result> element:

                    <param name="parse">false</param>


    <xwork key="my-work" name="download">
        <package name="download" extends="default" namespace="/admin/my-plugin">
            <default-interceptor-ref name="defaultStack"/>

            <action name="download" class="com.example.confluence.plugin.myplugin.actions.MyAction">
                <result name="download" type="stream">
                    <param name="contentType">text/csv; charset=UTF-8</param>
                    <param name="inputName">csv</param>
                    <param name="bufferSize">4096</param>
                    <!-- Atlassian's StreamResult extends the wrong class, so we must disable an irrelevant option. -->
                    <param name="parse">false</param>

Unfortunately, if you look at StreamResult, they don’t actually support all the parameters you would expect from looking at documentation for webwork or struts2. From what I can tell, only contentType, inputName, and bufferSize are used. The rest are set directly on the response with help from ServletResponseAware.

package com.example.confluence.plugin.myplugin.actions;

import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.xwork.Action;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

public class MyAction implements Action, ServletResponseAware {

    private final MyService myService;

    private InputStream csv;

    private HttpServletResponse response;

    public MyAction(final MyService myService) {
        this.myService = myService;

    public String execute() {
        final byte[] contents = myService.getCSV().getBytes(StandardCharsets.UTF_8);

        csv = new ByteArrayInputStream(contents);

        response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contents.length));
        response.addHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
        response.addHeader("Pragma", "no-cache");
        response.addHeader(HttpHeaders.EXPIRES, "-1");
        response.addHeader("Content-Disposition", "attachment; filename=\"my.csv\"");

        return "download";

    @SuppressWarnings("unused") // used by xwork
    public InputStream getCsv() {
        return csv;

    public void setServletResponse(final HttpServletResponse response) {
        this.response = response;


The path is: /admin/my-plugin/download.action

When hit (e.g. from a web-item), a file download dialog opens in Firefox. The filename is set properly and Firefox recognizes the mime type as “CSV document”.

This was performed using Atlassian SDK 8.0.16, Confluence 6.15.8, and the app is using Atlassian Spring Scanner 2.1.7. Names, packages, and other things have changed from my working code; so, please check for typos (mine and yours) before saying it doesn’t work!

1 Like

This is a how-to. See question for the answer.

1 Like