前台

  

<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. hdu 3183 A Magic Lamp 贪心

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  2. MaterialImageView

    https://github.com/zhaozhentao/MaterialImageView

  3. 使用JDBC 插入向数据库插入对象

    package com.ctl.util; import java.io.IOException; import java.lang.reflect.Field; import java.lang.r ...

  4. 第一个get请求的爬虫程序

    一:urllib库: urllib是Python自带的一个用于爬虫的库,器主要作用就是可以通过代码模拟浏览器发送请求.其被用到子模块在Python3中的urllib.request和urllib.pa ...

  5. 使用scanf_s报错:0xC0000005: Access violation writing location 0x00000000

    在vs2010中写了一行scanf("%s",name); 调式时 提示warning , 提示修改为scanf()使用可能会存在不安全,建议使用scanf_s() 但是我修改成s ...

  6. How to Execute Page_Load() in Page's Base Class?

    https://stackoverflow.com/questions/2737092/how-to-execute-page-load-in-pages-base-class We faced th ...

  7. 如何编写linux下nand flash驱动-2

    [Nand Flash引脚(Pin)的说明] 图3.Nand Flash引脚功能说明 上图是常见的Nand Flash所拥有的引脚(Pin)所对应的功能,简单翻译如下: 1.       I/O0 ~ ...

  8. RobotFrameWork--selenium2模拟chrome的user agent

    ${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver ${opt ...

  9. 小程序-demo:template

    ylbtech-小程序-demo: 1.返回顶部 1.app.js 2.app.json 3.app.wxss 4.project.config.json 5.pages 6.images 7. 2. ...

  10. linq to EF分组查询 group by 的使用

    第一种:查询表达式语法: IQueryable<EnrollmentDateGroup> data = from student in db.Students group student ...