前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【easypoi(二)使用模板导出爬坑经历】

【easypoi(二)使用模板导出爬坑经历】

作者头像
用户5640963
发布2020-02-25 17:05:00
3.3K0
发布2020-02-25 17:05:00
举报
文章被收录于专栏:卯金刀GG卯金刀GG

问题描述:

1、开发环境,idea开发工具,windows10系统,JDK8.0;系统部署环境,CentorOS7;

2、在开发的过程中,使用模板下载文档,因为模板是固定的,只需要填写需要的信息即可;

发现问题:

模板文档放置在项目的sources/word自定义的文件夹下面,在开发的过程中,能够顺利导出word文档,但是到服务器上就导出空文档;

排查过程:

1、猜想是不是保存的临时文件错误,于是通过打印输出的文件查看,文件夹存在,但是,没有生成预想的临时文件;方法如下

代码语言:javascript
复制
StringBuilder sbd = new StringBuilder();
boolean directory = dir.isDirectory();
if (directory) {
	File[] files = dir.listFiles();
	for (int i = 0; i < files.length; i++) {
		sbd.append(files[i].getName()+":"+files[i].getPath());
		sbd.append("&&&&");
	}
}

2、继续猜想,是不是resources/word的文档找不到呢;通过方法,可以获取到文档;当时直接通过file是不能获取到文件的,会报错;

File sourceFile = ResourceUtils.getFile("classpath:word/test.docx"); //这种方法在linux下无法工作

可以通过

ClassPathResource resource = new ClassPathResource("word/test.docx");

也可以通过

@Resource private ResourceLoader resourceLoader;

org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:word/test.docx");

可以断定,是easypoi中在引用临时文件的时候,找不到路径报错,那么我想是不是可以使用文件复制,把test文件copy到一个临时文件,再使用这个copy的文件呢,还真有这种方法;遂解决方法一实现,copy临时文件。以下贴出简单的方法;此处借用别人的方法;

代码语言:javascript
复制
/**
     *
     * @param path
     * @return
     */
    public static String convertTemplatePath(String path) {
        Resource resource = new ClassPathResource(path);
        FileOutputStream fileOutputStream = null;
        // 将模版文件写入到 tomcat临时目录
        String folder = System.getProperty("catalina.home");
        File tempFile = new File(folder + File.separator + path);
        // System.out.println("文件路径:" + tempFile.getPath());
        // 文件存在时 不再写入
        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) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return tempFile.getPath();
    }

问题解决;

3、以上的方法是解决的实际问题,但是多了copy的步骤,是不是应该还有更简便的方法;于是想到现在用的是springboot2,那么easypoi应该也有与之相关的版本,然后查看本项目引用的版本,如下;

代码语言:javascript
复制
<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-base</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-web</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-annotation</artifactId>
			<version>3.0.1</version>
		</dependency>

并不是最新版本,于是升级下版本

代码语言:javascript
复制
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.0.3</version>
</dependency>

或者直接引用easypoi-spring-boot-starter

代码语言:javascript
复制
<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-spring-boot-starter</artifactId>
			<version>3.3.0</version>
		</dependency>

然后再测试,遂完美解决。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档