1、添加依赖

 <dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.10</version>
</dependency>

2、将文件(init_dmconf_db.sql)放到resources目录下

读取resource下sql脚本并执行

 

3、增加工具类FileDownloadUtils和InitSqlUtil

1)FileDownloadUtils读取resources目录下的文件返回file

package com.dm.botconf.util;

import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class FileDownloadUtils {
    /**
     * @param fileName-文件在资源目录下的相对路径,路径必须不以“/”开头。如“static/a.txt”
     * @description: 从jar包内部的资源目录下下载文件
     */
    public static File download(String fileName) {
        InputStream input = null;
        try {
            Resource resource = new ClassPathResource(fileName);
            input = resource.getInputStream();
            File targetFile = new File(resource.getFilename());
            FileUtils.copyInputStreamToFile(input, targetFile);
            return targetFile;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
View Code

相关文章: