AutoPoi Linux上TemplateExportParams报空指针异常

项目场景

        Springboot使用AutoPoi模版注解方式导出数据


问题描述

        Springboot使用AutoPoi部署在Linux上TemplateExportParams类获取模版路径报空指针异常

String path = "static/exportTemplate/xxxx.xlsx";
TemplateExportParams templateExportParams = new TemplateExportParams(path);

解决方案

1、新建工具类,将模版写到临时目录下,再读取临时目录下的模板即可
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 获取模板路径工具类
 * 解决 linux 获取模版报空指针异常
 *
 * @author wangyiren
 * @version 1.0
 * @title TemplatePathUtils
 * @description
 * @date 2022/07/26 13:58
 */
@Slf4j
public class TemplatePathUtils {

    private TemplatePathUtils() {
    }

    private static final String OS_NAME = "os.name";

    private static final String OS_VERSION = "Windows";

    public static final String CATALINA_HOME = "catalina.home";

    public static String getTemplatePath(String path) {
        if (System.getProperties().getProperty(OS_NAME).contains(OS_VERSION)) {
            return path;
        }
        Resource resource = new ClassPathResource(path);
        FileOutputStream fileOutputStream = null;
        // 将模版文件写入到 tomcat临时目录
        String folder = System.getProperty(CATALINA_HOME);
        File tempFile = new File(folder + File.separator + path);
        // 文件存在时 不再写入
        if (tempFile.exists()) {
            return tempFile.getPath();
        }
        File parentFile = tempFile.getParentFile();
        // 判断父文件夹是否存在
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(resource.getInputStream());
            fileOutputStream = new FileOutputStream(tempFile);
            byte[] buffer = new byte[10240];
            int len = 0;
            while ((len = bufferedInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            log.error("获取文件路径异常,异常原因{}", e, e.getMessage());
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    log.error("获取文件路径关闭文件流异常,异常原因{}", e, e.getMessage());
                }
            }
        }
        return tempFile.getPath();
    }
}

2、调用工具类获取模版路径

String path = TemplatePathUtils.getTemplatePath("static/exportTemplate/XXXX.xlsx");
TemplateExportParams templateExportParams = new TemplateExportParams(path);

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王少邪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值