前台

  

<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实现模板文件下载的更多相关文章

  1. Spring Boot实现文件下载功能

    我们只需要创建一个控制器(Controler)文件,即Controller目录下的File_Download.java,其完整目录如下: @Controller public class File_D ...

  2. Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate(转)

    今天看了下Spring的源码——关于JDBC的"薄"封装,Spring 用一个Spring JDBC模板类来封装了繁琐的JDBC操作.下面仔细讲解一下Spring JDBC框架. ...

  3. Spring配置文件模板

    模板: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  4. Spring MVC 的文件下载

    在看Spring MVC文件下载之前请先看Spring MVC文件上传 地址:http://www.cnblogs.com/dj-blog/p/7535101.html 文件下载比较简单,在超链接中指 ...

  5. Spring 事务模板

    最近项目开发中需要用到单机事务,因为项目中使用了Spring和Mybatis框架,所以通过Spring来进行事务的管理,并且记录一下事务配置的过程 第一步:配置DataSource <!-- 发 ...

  6. 【Spring实战】—— 15 Spring JDBC模板使用

    前一篇通过对传统的JDBC的使用操作,可以体会到使用的繁琐与复杂,套句话说,是用了20%作了真正的工作,80%作了重复的工作. 那么通过本篇,可以了解如下的内容: 1 如何配置数据源 2 如何在spr ...

  7. IDEA中使用spring官方模板+@Controller

    视图层处理http请求用@Controller时,要配合模板的使用,模板类似javaweb中的jsp,但是模板的引擎用的是 thymeleaf ,但是并不推荐. 现在的开发模式都是前后端分离,做后端只 ...

  8. Spring AOP Example 文件下载:

      文件下载:http://files.cnblogs.com/wucg/spring_aop_excise.zip P:124 spring核心技术 P225: spring doc 可以把Advi ...

  9. Spring MVC实现文件下载

     下载文件① 下载文件需要将byte数组还原成文件. 首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式).然后使用OutputStream将文件输入 @RequestMapp ...

随机推荐

  1. 【转】TestNG执行顺序控制

    1.class执行顺序控制---testng.xml之preserve-order preserve-order:用来控制<test>里面所有<classes>的执行顺序.&l ...

  2. STL vector的介绍(1)

    尝试下翻译STL里面的一些easy和算法.四级过了.六级刚考.顺便练练自己的英语水平.翻译的不好的地方请大神多多不吝赐教哈.方便我改正. 原来均来自:http://www.cplusplus.com/ ...

  3. 配置 Apache 服务器禁止所有非法域名 访问自己的服务器

    .http2..1以前: 第一种 直接拒绝访问 打开 httpd.conf 文件,将一下配置追加到文件最后. <pre name="code" class="htm ...

  4. 在CentOS上把PHP从5.4升级到5.5

    在CentOS上把PHP从5.4升级到5.5 摘要:本文记录了在CentOS 6.3上,把PHP从5.4.8升级到5.5.13的过程. 1. 概述 在我做的一个项目中,最近我对生产服务器上的一系列系统 ...

  5. WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks

    What’s New Automatic Dimensions - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSect ...

  6. 反爬统计 数据库 sql CASE

    -- 经排查日志,发现ordertest.com下的url检测,频繁<Response [403]>,Forbidden;再进一步查询数据库数据:逐日统计错误临时表test_error_t ...

  7. 在win10 docker启动的centos容器中安装nginx

    我是在win10机器上搭建了一个docker,在docker启动了centos容器,在centos中安装nginx. 安装配置docker 直接在官网下载docker for windows:http ...

  8. HDU1087 Super Jumping! Jumping! Jumping! —— DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1087 Super Jumping! Jumping! Jumping! Time Limi ...

  9. Android开发之自定义对话框

    由于系统自带的对话框不好看,于是本人就自定义了一个对话框,以后有类似的就可以直接使用啦.先上效果图: 1,布局文件dialog_clear_normal.xml <?xml version=&q ...

  10. POJ - 2418 Hardwood Species(map,trie,BST)

    1.输入若干行树名,输入结束后,按字典序输出树名及其所占百分比. 2.多种方法:map,trie,BST 3. map: #include<iostream> #include<st ...