SpringBoot下载Excel文件,解决文件损坏问题

最近,在做一个springboot下载excel文件的模块,下载下来的文件一直是文件受损,网上找了一堆解决办法都不行,墨迹了半天终于解决了,主要原因就是springboot的resource目录下的文件是默认自动压缩的,所有直接下载打开会出错,这里记录一下解决方法

先上代码

public void downloadExcel(HttpServletResponse response,HttpServletRequest request) {
        try {
            //获取要下载的模板名称
            String fileName = "ruleConfig.xlsx";//ruleConfig.xlsx
            //设置要下载的文件的名称
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            //通知客服文件的MIME类型
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            //获取文件的路径
            String filePath = getClass().getResource("/static/" + fileName).getPath();
            System.out.println(filePath);
            FileInputStream input = new FileInputStream(filePath.substring(1));
            OutputStream out = response.getOutputStream();

           byte[] b = new byte[2048];
            int len;
            while ((len = input.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
            out.close();
            input.close();
        } catch (Exception ex) {
            log.error("getApplicationTemplate :", ex);
            //return Response.ok("应用导入模板下载失败!");
        }
    }

这就是普通的下载的代码

解决办法

重点是下面在pom中添加避免压缩的插件

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<version>2.6</version>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
					<nonFilteredFileExtensions>
						<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
					</nonFilteredFileExtensions>
				</configuration>
			</plugin>

这个插件可以避免xlsx文件在resource目录下被自动压缩,这样就可以正常下载,打开了

  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
要在Spring Boot中下载Excel文件,可以使用Apache POI库来创建和写入Excel文件,然后使用Spring MVC的ResponseEntity来将文件作为响应发送给客户端。 以下是一个简单的示例代码: ```java @GetMapping("/downloadExcel") public ResponseEntity<Resource> downloadExcel() throws IOException { // 创建Excel文件 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); Row row = sheet.createRow(); Cell cell = row.createCell(); cell.setCellValue("Hello, World!"); // 将Excel文件写入到临时文件中 File tempFile = File.createTempFile("temp", ".xlsx"); FileOutputStream outputStream = new FileOutputStream(tempFile); workbook.write(outputStream); outputStream.close(); // 创建Resource对象,将临时文件作为内容 Resource resource = new FileSystemResource(tempFile); // 创建响应对象,设置Content-Disposition头部信息 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=example.xlsx"); // 返回响应对象 return ResponseEntity.ok() .headers(headers) .contentLength(tempFile.length()) .contentType(MediaType.parseMediaType("application/vnd.ms-excel")) .body(resource); } ``` 在这个例子中,我们首先使用Apache POI库创建了一个Excel文件,然后将其写入到一个临时文件中。接下来,我们创建了一个Resource对象,将临时文件作为内容。最后,我们使用ResponseEntity将Resource对象作为响应发送给客户端,并设置Content-Disposition头部信息,以便浏览器能够将响应保存为文件。 希望这可以帮助你实现在Spring Boot中下载Excel文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值