Reputation: 41
Code tried in jsp given below .The code below opens the window explorer in to save the file to .xlsx (using chrome browser)but the requirement is to auto download the file while clicking the icon .tried solutions of the similar question posted in stack overflow but could not find a correct solution to the question.
Solution checked in browser Firefox and in chrome
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
response.setContentType("application/application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet");
response.setHeader ("Content-Disposition",
"attachment;fileName=tempAuthorizationCloseOutReportsResult.xlsx;");
%>
<s:set var="resultList" value="#request.tempAuthorizationCloseoutResult" />
<div>
<h3></h3>
</div>
<br/>
<s:if test="#resultList.size <= 0">
<table>
<tr>
<td>
<b> <s:text name="ui.label.text.norecordsfound" /> </b>
</td>
</tr>
</table>
</s:if>
<s:elseif test="#resultList.size> 0">
<table>
<tr>
<td>
<b><s:text name="ui.label.text.totalnumberofrecordsfound"/> :
<s:property value="#resultList.size" /> </b>
</td>
</tr>
</table>
Upvotes: 2
Views: 15739
Reputation: 473
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
File xfile = < File>
try (BufferedOutputStream bfos = new BufferedOutputStream(response.getOutputStream());
FileInputStream fs = new FileInputStream(xfile)) {
byte[] buffer = new byte[fs.available()];
fs.read(buffer);
bfos.write(buffer, 0, buffer.length);
bfos.flush();
}
}
Upvotes: 3