当前位置: 首页 > news >正文

下载工程resources目录下的模板excel文件

一、添加依赖

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>

二、编写接口

@GetMapping("/downloadTemplate")public void downloadTemplate(HttpServletRequest request,HttpServletResponse response){String fileName = "template"+File.separator+"字段标准导入模板.xlsx";InputStream is = ClassLoader.getSystemResourceAsStream(fileName);response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");Workbook wb = null;try {wb = new XSSFWorkbook(is);} catch (IOException e) {throw new RuntimeException(e);}try {wb.write(response.getOutputStream());} catch (Exception e) {log.error("下载导入模板异常{}", e.getMessage());} finally {IOUtils.closeQuietly(wb);if(is!=null){try {is.close();} catch (IOException e) {throw new RuntimeException(e);}}}}


三、这种代码,在windows上调试没问题,打成jar包发到linux环境上会有问题,改成如下代码就行:

@GetMapping("/downloadTemplate")public void downloadTemplate(HttpServletRequest request,HttpServletResponse response){String fileName = "template"+File.separator+"ColumnStandardImportTemplate.xlsx";response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setCharacterEncoding("utf-8");try (InputStream is = new ClassPathResource(fileName).getInputStream();Workbook wb = new XSSFWorkbook(is);ServletOutputStream os = response.getOutputStream()) {wb.write(os);os.flush();} catch (Exception e) {log.error("下载导入模板异常", e);}}


    

http://www.lryc.cn/news/376102.html

相关文章:

  • 音频基础知识和音频指标
  • 使用Vue CLI在其他磁盘创建项目出现错误及解决
  • 关于lamda表达式的使用
  • Android替换默认的按键音
  • Windows 服务器Nginx 下载、部署、配置流程(图文教程)
  • 基于Redis实现共享session登录
  • shell函数的定义
  • vue部署宝塔nginx配置(获取用户ip地址、反代理访问api接口、websocket转发)
  • Jenkins教程-3-github自动化测试任务构建
  • 0元体验苹果macOS系统,最简单的虚拟机部署macOS教程
  • Codeforces Round 946 (Div. 3) E. Money Buys Happiness
  • Git记录 上传至Gitee
  • 笔记-前端
  • 事务AOP
  • RAM和ROM
  • 聊聊系统架构之负载均衡优化实践
  • 代码规范性思考
  • TestProject Python SDK入门
  • 服务器数据恢复—EMC Isilon存储中被误删的虚拟机数据恢复案例
  • 华为安全Security认证,你了解多少?
  • 自动驾驶规划-RTT* 算法 【免费获取Matlab代码】
  • shell编程中的运算符的讲解
  • yudao-ui-admin-vue3 nginx配置
  • vue3第四十节(pinia的用法注意事项解构store)
  • PostgreSQL源码分析——索引扫描
  • 零基础入门学用Arduino 第四部分(一)
  • x-anylabelimg如何标识人脸
  • Element-ui中Table表格无法显示
  • 电信网关配置管理系统 del_file.php 前台RCE漏洞复现
  • 游戏心理学Day18