有沒有一些技巧讓SpringMVC自定義視圖導致在瀏覽器中下載文件?我已經從org.springframework.web.servlet.View
實現了render
方法,但代碼導致我的數據作爲數據blob寫入頁面,而不是啓動下載操作。spring-mvc下載動作
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測試。它也寫入服務器日誌並完成。數據在瀏覽器中結束。根據螢火蟲,HTTP Headers看起來很正常。 catch塊中沒有錯誤出現在服務器日誌中。
缺少什麼我在這裏?
您是否嘗試過亂搞內容類型,以查看如果將其更改爲其他內容(也應該可下載),如application/octet-stream會發生什麼? – Jherico 2009-09-04 21:09:19
嘗試'out = response.getWriter();' – serg 2009-09-04 21:31:41