最近在項(xiàng)目當(dāng)中遇到模板下載的問(wèn)題:當(dāng)服務(wù)器為T(mén)omcat的時(shí)候文件可以正常下載,但是當(dāng)放到線上(WebLogic服務(wù)器)下載的模板為空,現(xiàn)記錄下解決辦法。
public void importSuccess() throws Exception {
try {
HttpServletResponse response = Struts2Utils.getResponse();
// 重置響應(yīng)
response.reset();
// 設(shè)置響應(yīng)頭為二進(jìn)制流
response.setContentType("application/x-msdownload;charset=utf-8");
response.setContentType("APPLICATION/OCTET-STREAM ");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=ComplaintsImportModel.xls");
String path = this.getClass().getClassLoader().getResource("/")//這里才是問(wèn)題的關(guān)鍵,WebLogic服務(wù)器要在讀取添加一個(gè)"/"
.getPath();
path = path.substring(1, path.length());
String name = File.separator + path + "ComplaintsImportModel.xls";
logger.info("**********************************" + name
+ "*******************************");
InputStream is = new FileInputStream(name);
HSSFWorkbook wb = new HSSFWorkbook(is);
// 得到 文件流
OutputStream out = response.getOutputStream();
wb.write(out);
is.close();
out.flush();
out.close();
} catch (Exception e) {
logger.error("下載導(dǎo)出模版失敗", e);
}
}