广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot下载Excel文件时,报错文件损坏的解决方案
  • 696
分享到

SpringBoot下载Excel文件时,报错文件损坏的解决方案

2024-04-02 19:04:59 696人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

SpringBoot下载excel文件文件损坏 我把模板文件放在了resources目录下 Maven插件打包项目的时候,默认会压缩resources目录下的文件。 服务器读取的文

SpringBoot下载excel文件文件损坏

我把模板文件放在了resources目录下

Maven插件打包项目的时候,默认会压缩resources目录下的文件。

服务器读取的文件流来自于压缩后的文件,而返回给浏览器时,浏览器把他当作正常的文件解析,自然不能得到正确的结果。

解决方案:

配置一下maven插件,打包的时候不要压缩模板文件,排除拓展名为xlsx的文件。


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

即使这里配置了utf-8,也会出现文件的中文名乱码的情况。

想彻底解决乱码问题,我们还需要在代码中需要做一些处理。

下面贴一个工具类,看大概思路即可。


package com.zikoo.czjlk.utils; 
import com.zikoo.czjlk.exception.EmServerError;
import com.zikoo.czjlk.exception.EmServerException; 
import javax.servlet.Http.httpservletResponse;
import java.io.*;
import java.net.URLEncoder; 
public class FileUtils { 
    public static void download(HttpServletResponse response, String filePath, String fileName){
 
        try {
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
 
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
 
            writeBytes(is, response.getOutputStream());
        }catch (Exception e) {
            throw new EmServerException(EmServerError.FILE_OPERATION_ERROR);
        }
    }
 
    private static void writeBytes(InputStream is, OutputStream os) {
        try {
            byte[] buf = new byte[1024];
            int len = 0;
            while((len = is.read(buf))!=-1)
            {
                os.write(buf,0,len);
            }
        }catch (Exception e) {
            throw new EmServerException(EmServerError.FILE_OPERATION_ERROR);
        }finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

springBoot项目中,下载文件出现异常:

SpringBoot下载文件,出现异常:Could not find acceptable representation

在这里插入图片描述

接口定义为:


public XResponse<Void> exportProject(@PathVariable("projectId") String projectId,
         HttpServletResponse response) throws Exception 

原因:在下载文件时,接口不能有返回值

将接口定义修改为:


public void exportProject(@PathVariable("projectId") String projectId,
         HttpServletResponse response) throws Exception 

此时下载就没有异常信息了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: SpringBoot下载Excel文件时,报错文件损坏的解决方案

本文链接: https://www.lsjlt.com/news/128952.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作