[easypoi uses template to derive climbing experience]

Problem Description:

1. Development environment, idea development tool, windows10 system, JDK8.0; system deployment environment, centeros7;

2. In the process of development, use the template to download documents, because the template is fixed, just fill in the required information;

Problems found:

Template documents are placed under the sources/word customized folder of the project. During the development process, word documents can be successfully exported, but empty documents can be exported to the server;

Troubleshooting process:

1. Guess whether the saved temporary file is wrong, so through the printout file view, the folder exists, but the expected temporary file is not generated; the method is as follows

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. Continue to wonder if the resources/word document can't be found. You can get the document through methods. At that time, you can't get the file directly through file, and an error will be reported;

File sourceFile = ResourceUtils.getFile("classpath:word/test.docx"); / / this method cannot work under linux

Through

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

Or through

     @Resource private ResourceLoader resourceLoader;

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

It can be concluded that when the temporary file is referenced in easypoi, no path error can be found, so I think it's possible to copy the test file to a temporary file, and then use the copy file. There's really such a way. So the first solution is to copy the temporary file. Here are some simple ways to use others' methods;

/**
     *
     * @param path
     * @return
     */
    public static String convertTemplatePath(String path) {
        Resource resource = new ClassPathResource(path);
        FileOutputStream fileOutputStream = null;
        // Write template file to tomcat temporary directory
        String folder = System.getProperty("catalina.home");
        File tempFile = new File(folder + File.separator + path);
        // System.out.println("file path:" + tempFile.getPath());
        // No longer write when file exists
        if (tempFile.exists()) {
            return tempFile.getPath();
        }
        File parentFile = tempFile.getParentFile();
        // Determine whether the parent folder exists
        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();
    }

Problem solving;

3. The above method is to solve the actual problem, but if there are more copy steps, there should be a simpler way. So I think that spring boot2 is used now, so easypoi should also have the relevant version, and then check the version referenced by this project, as follows:;

<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>

It is not the latest version, so upgrade to the next version

<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>

Or directly refer to easypoi spring boot starter

<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-spring-boot-starter</artifactId>
			<version>3.3.0</version>
		</dependency>

Then test it again and solve it perfectly.

Tags: Programming Spring Linux Tomcat

Posted on Thu, 20 Feb 2020 22:51:41 -0500 by Myke