How to determine StreamResult output name in Struts2?

Guys, I can’t find this information on the Internet. I have an action and I generate a text file, however, it is always displayed by the client as the file "generatePDF.action". I want it to appear as a .txt receipt file.

Here is my annotation:

   @Action(value = "/generateTXT",
    results = {
        @Result(name = "ok", type = "stream",
        params = {"inputName", "inputStream",
                  "contentType", "application/octet-stream",
                  "contentDispostion", "attachment;filename=receipt.txt"})
    })
+5
source share
3 answers

If you are using the legend plugin, then use the following code for reference runs in the section "/ YourApplicationContext / stream / stream-test", which then resolves to /YourApplicationContext/stream/document.txt:

package struts2.stream;

import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.apache.struts2.convention.annotation.Result;


@Result(name = ActionSupport.SUCCESS, type = "stream", params =
{
    "contentType",
    "text/hmtl",
    "inputName",
    "inputStream",
    "contentDisposition",
    "filename=document.txt"
})
public class StreamTestAction extends ActionSupport{
    public InputStream inputStream;

    @Override
    public String execute(){
    inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");      
    return SUCCESS;
    }
}

, "contentDisposition" "filename = 'document.txt" "change' document.txt" , .

+5

, :

"contentDispostion" "contentDisposition"

, , , : -)

0

, :

@Result(name="export", type="stream",
   params={ "contentType", "application/octet-stream",
    "inputName", "fileInputStream",
    "contentDisposition", "attachment;filename=%{exportFilename}",
    "bufferSize", "4096"})

exportFilename - String getter setter, , ExportAction .

, .

0
source

All Articles