1

I'm using Struts2, Spring, and Hibernate.

I have written a simple Excel file called test.xls with POI. When I run the action, it works because test.xls appears, but in my tomcat folder.

I want that file to be downloadable on my jsp page. How do I go about getting the user to download the .xls file? Rather, how do I call that path?

Sorry if the answer is obvious, I am new to this. I know it probably has something to do with the ServletContext or HttpServletRequest?

[edit] I got it working thanks to the below answers. Here is the working code:

private InputStream excelStream;

public String export(){
//Create excelfile
    HSSFWorkbook workbook = new HSSFWorkbook();
    FileOutputStream file = null;

    try{
         file = new FileOutputStream("poi-test.xls"); 
    }
    catch(IOException e){
        e.printStackTrace();
    }       
//Populate workbook with all the excel stuff
    ...

//Write to file
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try{
        workbook.write(file);
        workbook.write(baos);
        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); 
        excelStream = bis;
    }
    catch(IOException e){
        e.printStackTrace();
    }
    return "excel";
}

public InputStream getExcelStream() {
    return excelStream;
}


public void setExcelStream(InputStream excelStream) {
    this.excelStream = excelStream;
}

Over in my struts file:

        <result name="excel" type="stream">
            <param name="contentType">application/vnd.ms-excel</param>
            <param name="inputName">excelStream</param>
            <param name="contentDisposition">attachment; filename="excelExport.xls"</param>
            <param name="bufferSize">1024</param>
        </result>
yuudachi
  • 2,266
  • 3
  • 17
  • 17
  • I removed the hibernate and spring tags since this question is unrelated to those technologies. – Steven Benitez Jan 29 '11 at 02:19
  • Hi yuudachi, I am currently trying to implement a similar scenario but am facing problems as I am new to this part. I am referring to the second link provided by @steven . It would be nice if you could post the working code or a working example for this, so that I can figure out where I have gone wrong. Thanks!! – kanishk Feb 15 '12 at 11:49
  • I updated my question with my working code – yuudachi Feb 24 '12 at 19:34

2 Answers2

1

You want to use the Struts2 Stream Result for this. Here is some information on that:

Community
  • 1
  • 1
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
1

If the newly created Excel file is accessible from the web server, why not just present that HTTP link to your Excel file in your JSP page? Unless you want to stream the excel file dynamically rather than physically creating it like you are currently done, this is probably the most straightforward solution.

limc
  • 39,366
  • 20
  • 100
  • 145
  • Its in my tomcat folder, I don't know how I would access that. Can I change where it ends up being created? – yuudachi Jan 29 '11 at 03:33
  • If your current project is running in Tomcat, chances are the project folder resides in tomcat's htdoc folder unless you specifically create a context path pointing to your project elsewhere. Just drop the excel file there, then you can access it through http://server/yourapp/excel.xls . I'm not saying this is the recommended solution, but it will allow you to do what you want easily. – limc Jan 29 '11 at 03:38
  • I ended up using Steven's answer, but your answer helped clarify some things that I wasn't understanding. Thank you! – yuudachi Jan 30 '11 at 08:48