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>