I have this controller which create an empty sheet and I want to return the excel file to the navigator. The problem is, the excel file is corrupted.
If I create the file on my computer the file isn't corrupted, so my HSSFWorkbook is valid. Seems a problem of encodage/encapsulation added by the spring context ?
@Controller
public class ExportController {
@RequestMapping(value = "/export/test/excel", method = RequestMethod.POST)
public void downloadExcelTestFile(
HttpServletRequest request,
HttpServletResponse response) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("Sheet1");
//response.reset();
//response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=test.xls");
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
wb.close();
}
The download start well, I receive the file test.xls, but I can't open it. Is there a Spring way to achiev a proper download inside a @Controller ?
I use Spring 4.2.4
UPDATE 1
I tried a Spring way but it's not working better
HSSFWorkbook wb = new HSSFWorkbook();
wb.createSheet("Sheet1");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
wb.write(bos);
} finally {
bos.close();
}
byte[] bytes = bos.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/vnd.ms-excel;");
headers.set("content-length",Integer.toString(bytes.length));
headers.set("Content-Disposition", "attachment; filename=test.xls");
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
UPDATE 3
I found a reason but I don't understand why.
If I build my war file and deploy it manually in the very same tomcat 7.0.70 it works. My Excel is not corrupted.
If I download from the dev environnement in eclipse, it doesn't work. Seems a tomcat + eclipse issue.