1

I want to download xml file with my struts application. I recup my xml in JAVA (in my action) . When I click to my link, the download window doesn't open . This my code : ACTION :

@Action(value = "download", results = {
            @Result(name = "success", type = "redirectAction", params = {
                    "actionName", "testMM"}),
            @Result(name = "input", location = "testMM.jsp") })
    public String dowload() {
        setFilePath(getSession().get("filePath", String.class));
        setFileName(getSession().get("fileName", String.class));

        try {
            fileInputStream = new FileInputStream("c://mm.xml");



        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return SUCCESS;
    }

/**
     * @return the fileInputStream
     */
    public InputStream getFileInputStream() {
        return fileInputStream;
    }

MY JSP:

<s:url id="fileDownload" namespace="/adminMM" action="download" ></s:url>
<s:a  style="width: 300px; maring: 15px 25px;" href="%{fileDownload}"><s:text name="%{resultFileName}" /></s:a>

Please I need help . I can give you more information .

MR67
  • 23
  • 3

1 Answers1

0
  1. When downloading a file with Struts2, you need to use the Stream result type:

    @Action(value = "download", 
        results = {
            @Result(name = "success", type = "stream", params = {
                    "contentType"        , "text/xml",
                    "inputName"          , "fileInputStream",
                    "contentDisposition" , "attachment;filename=\"foobar.xml\""
            }),
            @Result(name = "input", location = "testMM.jsp") 
        }
    )
    
  2. Return an ERROR in case of file not found, you are just printing the stacktrace and returning SUCCESS the same;

  3. Correct the typo in the dowload method that must be download.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243