标签:stack classpath cat gb2312 targe put res write file
前台
<form id="batchModel0" method="post" action="/common/download-file" target="downLoadFile"> <table> <tr> <td> <a href="#" id="downLoad">通过手机号码查询工号名称</a> <input type="hidden" id="remoteFileName" name="remoteFile" value="通过手机号码查询工号名称.xlsx"> <button class="btnTip " href="#execlNoteSJHM" name="mouldDetail" arrow="false" id="execlMouldSJHM" type="button" /> </td> </tr> </table> </form>
后台
@RequestMapping(value = "common/download-file") public void downloadFile(String remoteFile, HttpServletResponse response) { try { // excel模板路径, 此模板是直接放到项目的文件夹中即可 File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "resources/batch-excel/" + remoteFile); response.reset(); response.setContentType("application/octet-stream"); // 设置response中的文件内容 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Content-Disposition", "inline; filename=" + new String((remoteFile).getBytes("gb2312"), "ISO8859-1")); OutputStream os = response.getOutputStream(); InputStream is = new FileInputStream(file); try { byte[] bytes = new byte[1024]; while (is.read(bytes) > 0) { os.write(bytes); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { os.flush(); is.close(); os.close(); } } catch (IOException e) { e.printStackTrace(); } }
标签:stack classpath cat gb2312 targe put res write file
原文地址:https://www.cnblogs.com/qiaoyutao/p/9854556.html