SpringBoot读取resource下文件

版权声明:这里只是小白学习记录笔记资源的位置,若有问题请大佬们指点(●'◡'●) https://blog.csdn.net/weixin_42448414/article/details/85756443

避免了在linux下获取失败问题

/********************************
     *
     * @Description  读取文件,避免了在linux下 classpath 不起作用问题
     * @MethodName   readFile
     * @param        readFileUrl
     * @return       java.io.File
     * @Author       fancw
     * @Date         2019/1/3  9:43
     *
     *******************************/
    private static File readFile(String readFileUrl) {
        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:" + File.separator + readFileUrl);
        } catch (FileNotFoundException e) {
            try {
                file = ResourceUtils.getFile("file:" + File.separator + readFileUrl);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
        }
        return file;
    }

猜你喜欢

转载自blog.csdn.net/weixin_42448414/article/details/85756443