后台
@RequestMapping(value = "download", method = RequestMethod.GET)
public void downloadFile(/*(@RequestParam(value = "filename")file file,*/HttpServletResponse response,HttpServletRequest request){
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
String fileName="模板数据.xls";
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
try {
File file=new File(fileName);
System.out.println(file.getAbsolutePath());
String realpath=request.getSession().getServletContext().getRealPath("WEB-INF/resource/template/print/");
InputStream inputStream=new FileInputStream(realpath+"/"+fileName);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0){
os.write(b,0,length);
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
前台
function download_onClick(){
var link="<%=request.getContextPath()%>/cmerexpbill/download"
window.open(link);
return false;
};