码迷,mamicode.com
首页 > 编程语言 > 详细

spring实现模板文件下载

时间:2018-10-26 10:46:37      阅读:151      评论:0      收藏:0      [点我收藏+]

标签: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();
        }
    }

 

spring实现模板文件下载

标签:stack   classpath   cat   gb2312   targe   put   res   write   file   

原文地址:https://www.cnblogs.com/qiaoyutao/p/9854556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!