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文件夹如下,问题解决

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

相关推荐
桦说编程7 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen7 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研7 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员7 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋8 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国8 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~8 小时前
《设计模式》装饰模式
java·设计模式
A尘埃8 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交9 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
CHEN5_029 小时前
【Java基础面试题】Java基础概念
java·开发语言