在浏览器中使用SpringMVC自定义视图来下载文件有什么窍门吗?我已经从org.springframework.web.servlet.View实现了render方法,但是代码导致我的数据作为一个blob数据写入页面,而不是启动一个下载操作。
try {
Document oDoc = (Document) model.get("oDoc");
out = new PrintWriter(response.getOutputStream());
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-disposition", "attachment; filename=file.xls");
GenerateXLSFile gof = new GenerateXLSFile();
gof.outputTSVFromDom(out, oDoc);
} catch block here {
//writes to log here
} finally {
if (out != null) {
out.flush();
out.close();
}
}我知道渲染方法是从服务器日志中调用的。我知道GenerateXLSFile是从服务器日志创建的。我知道outputTSVFromDom可以从我的JUnit测试中获取文档并将其转换为工作。它还会写入服务器日志并完成。数据最终进入浏览器。根据firebug,HTTP标头看起来很正常。服务器日志中没有catch块中的错误。
这里我漏掉了什么?
发布于 2009-09-13 11:58:58
首先,您使用的是哪种API?Excel文档是二进制的,因此您应该使用OutputStream,而不是Writer。
其次,Spring内置了对Excel文档的支持:
基于Apache POI API
的JExcel接口
https://stackoverflow.com/questions/1381499
复制相似问题