Java实现Excel模板下载以及遇到的问题

Java实现Excel模板下载以及遇到的问题

前言:

项目在开发过程中,会用到Excel的导入,导出,复杂一点的Excel可以写好放在项目指定位置,下载时候直接从指定位置获取即可。

代码实现

excel存放的位置:

controller

c 复制代码
 @GetMapping("downloadxxxTemplate")
 @ApiOperation("下载xx-xx导入模板")
 public void downloadxxxTemplate(@RequestParam(value = "fileName") String fileName, HttpServletResponse response) throws IOException {
        dutyRosterService.downloadxxxTemplate(fileName,response);
    }

service

c 复制代码
   /**
     * 下载xxx-xx信息导入模板
     * 
     * @param fileName
     *            文件名
     * @param response
     *            响应
     * @throws IOException
     *             异常
     */
void downloadxxxTemplate(String fileName, HttpServletResponse response) throws IOException;

serviceImpl

c 复制代码
  public void downloadxxxTemplate(String fileName, HttpServletResponse response) throws IOException {
        InputStream inputStream = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
        	// 从类路径中的 excel 目录下获取名为 fileName.xlsx 的 Excel 文件
            inputStream = this.getClass().getClassLoader().getResourceAsStream("excel/" + fileName + ".xlsx");
            if (inputStream == null) {
               // 异常
            }
            bis = new BufferedInputStream(inputStream);
            fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
            // 设置文件下载头
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            // 设置文件ContentType类型,这样设置,会自动判断下载文件类型
            response.setContentType("multipart/form-data");
            bos = new BufferedOutputStream(response.getOutputStream());
            int len = 0;
            while ((len = bis.read()) != -1) {
                bos.write(len);
                bos.flush();
            }
            bos.close();
            bis.close();
            inputStream.close();
        } catch (IOException e) {
        	// 异常
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (bis != null) {
                bis.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

遇到的问题:

问题一:

excel 存放在resources的excel目录下,一直没有获取到报空指针异常,如下图,但是修改后还是空

从类路径中获取指定名称的资源,并返回一个输入流用于读取该资源的内容。

改为:this.getClass().getClassLoader().getResourceAsStream("excel/" + fileName + ".xlsx");

问题二:

打包 无论是package还是install 在target中excel都没有打进去,如下图:

最后找资料需要在maven依赖添加如下配置即可:

c 复制代码
	
    <build>
        <resources>
        	// 用自己话理解 可能有误
        	// 配置maven打包时候,将指定目录下面的文件打到target里面 
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
	//  maven打包Excel等资源时候,使用maven的filter,导致打包的Excel文件乱码或者损坏添加如下配置即可
		<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                        <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>

再次package时,target多个excel文件夹如下,问题解决

喜欢我的文章的话,点个阅读或者点个点赞,是我编写博客的动力,持续更新中

相关推荐
LuTshoes4 分钟前
spring ai 实战RAG(4)-模块化RAG
java·人工智能·spring
事圆则缓36 分钟前
Java I/O、文件与 Android 存储
java
wno70442 分钟前
Spring Security基础使用
java·后端·spring
小蒜学长1 小时前
基于RFID技术的仓储管理系统设计(代码+数据库+LW)
java·数据库·spring boot·后端·rfid技术·仓储管理
梦影_1 小时前
Langchain简单快速上手教程(五)——聊天模型之流式传输
java·数据库·人工智能·python·langchain
数据狐(Datafox)2 小时前
1688商品列表API接口解析(附 JSON 样例)
java·开发语言·数据库·爬虫·json
xcl09252 小时前
健身场馆无人自动化系统:从架构设计到落地实践
java·spring boot
EatFan2 小时前
一个二维码背后的系统设计:批次生成、绑定、扫码与数据统计怎么做?
java·后端·微信小程序·二维码·qrcode
白远山2 小时前
自助健身小程序源码:架构拆解、核心链路与本地部署实战
java·架构·uni-app·需求分析
Lyyaoo.2 小时前
【动态规划】【待更新】
java·数据结构·算法