之前在html页面已经实现了一个点击按钮下载excel模板文件的功能,现在新增业务需求,需要再增加一个按钮实现下载word模板文件,经过一周的摸索,现在给出一个简单实现的方案。为了实现该功能,改动的地方也挺多的
首先是html页面的,以下是关键代码部分,注意form表单中没有提交的url,由js动态提供
<form class="form-horizontal" id="form" method="post" οnsubmit="return false" enctype="multipart/form-data"> <input id="token" name="token" hidden> <!--由于后台才用的基于springboot spring security框架,且采用token作为连接状态保持--> 。。。省略其他非关键html代码 <button class="layui-btn layui-btn-normal" style="margin-left: 10px!important;" id="downTemp">下载excel模板</button> <button class="layui-btn layui-btn-normal" style="margin-left: 10px!important;" id="downTemp2">下载word模板</button> </form>
以下为js部分
$("#downTemp").on("click",function () {//下载excel模板 $("#token").val(localStorage.getItem("token"));//token存储在login.html页面实现 let formSel=$("#form"); formSel.attr("action","/files/downCardApplyTemp");//在这里动态提供url formSel.attr("onsubmit", "return true"); formSel.submit(); formSel.attr("onsubmit", "return false"); }); //下载word模板 $("#downTemp2").on("click",function () {//下载word模板 $("#token").val(localStorage.getItem("token")); let formSel=$("#form"); formSel.attr("action","/files/downloadWord"); formSel.attr("onsubmit", "return true"); formSel.submit(); formSel.attr("onsubmit", "return false"); });
接下来是java代码部分
controller层
//下载上岗证申请、审核信息word模板 @PostMapping("/downloadWord") @PreAuthorize("hasAuthority('sys:org:edit')")//该为spring security对应的注解,没用此框架的可以删除该注解 public void downloadWord(HttpServletResponse resp){ try{ String fileURL=filesPath+"XXXXXXXXX.doc"; WordUtil.downTemp(resp,fileURL); }catch (Exception ex){ ex.printStackTrace(); } } @PostMapping("/downCardApproveTemp") @PreAuthorize("hasAuthority('sys:org:edit')") public void downCardApproveTemp(HttpServletResponse resp){ try { String fileURL=filesPath+"XXXXXXXXXXX.xls"; ExcelUtil.downTemp(resp,fileURL); }catch (Exception ex){ ex.printStackTrace(); } }
//具体实现utils的代码
//下载excel模板的
public static void downTemp(HttpServletResponse resp,String fileURL) throws IOException { File file = new File(fileURL); // 取得文件名。 String filename = file.getName(); //String strName = new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(fileURL)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response resp.reset(); // 设置response的Header resp.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO-8859-1")); resp.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(resp.getOutputStream()); resp.setContentType("application/vnd.ms-excel;charset=gb2312"); toClient.write(buffer); toClient.flush(); toClient.close(); }
//下载word
//下载word模板 public static void downTemp(HttpServletResponse resp,String fileURL) throws IOException { File file = new File(fileURL); // 取得文件名。 String filename = file.getName(); resp.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO-8859-1")); //输入流为项目文件,输出流指向浏览器 byte[] buffer = new byte[1024]; FileInputStream fis=null; BufferedInputStream bis=null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = resp.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } }catch (Exception ex){ ex.printStackTrace(); } if(bis!=null){ bis.close(); } if(fis!=null){ fis.close(); } }