<a href="user/downloadusertmeplate">download template</a>
方法二:
<button type="button" class="btn btn-primary" id="downloadtemplate">doanload</button>
$("#downloadtemplate").click(function(){
var link=serverUri+"/user/downloadusertmeplate";
window.open(link);
return false;
});
java:
@ResponseBody
@RequestMapping("/downloadusertmeplate")
public void downloadUserTemplate(HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + "Design Review Exported.xlsx");
try {
System.out.println("File Path:"+File.separator+"Design Review Exported.xlsx");
InputStream inputStream = UserController.class.getResourceAsStream("/Design Review Exported.xlsx");
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
os.flush();
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}