Java 获取resources下的文件路径和创建临时文件

       之前处理根据模板文件,批量导入xxx.zip 的下载功能,用到这两个知识,就简单记录下,对于流的处理就跳过了      

由于maven项目打包会把 src/main/java 和 src/main/resources 下的文件放到 target/classes 下,所以统一以根路径代表此目录。

创建一个springboot项目

     《Java 获取resources下的文件路径和创建临时文件》 《Java 获取resources下的文件路径和创建临时文件》

server:
  port: 80
  servlet:
    context-path: /JQ_Resource

 

一、获取resources下的文件路径

总结起来有两点:

1、Class.getResource()的获取资源路径

      – 如果以 / 开头,则从根路径开始搜索资源。

      – 如果不以 / 开头,则从当前类所在的路径开始搜索资源。

2、ClassLoader.getResource()的资源获取不能以 / 开头,统一从根路径开始搜索资源。

String path = this.getClass().getClassLoader().getResource("xxx").getPath();

测试:

    public void getResource() {
        //1、通过Class的getResource方法
        String a1 = RescourceController.class.getResource("/cn/jq/jqresource/pojo/User.class").getPath();
        String a2 = this.getClass().getResource("../pojo/User.class").getPath();
        String a3 = RescourceController.class.getResource("/static/a.txt").getPath();
        String a4 = this.getClass().getResource("../../../../static/a.txt").getPath();
        System.out.println(a1.equals(a2)); // true
        System.out.println(a4); // /D:/JQ/workspace/JQ_Resource/target/classes/static/a.txt

        // 2、通过本类的ClassLoader的getResource方法
        String b1 = RescourceController.class.getClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();
        String b2 = this.getClass().getClassLoader().getResource("static/a.txt").getPath();
        String b3 = this.getClass().getClassLoader().getResource("static/resource/jq.docx").getPath();

        // 3、通过ClassLoader的getSystemResource方法
        String c1 = ClassLoader.getSystemClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();
        String c2 = ClassLoader.getSystemClassLoader().getResource("static/a.txt").getPath();
        String c3 = ClassLoader.getSystemClassLoader().getResource("static/resource/jq.docx").getPath();

        // 4、通过ClassLoader的getSystemResource方法
        String d1 = ClassLoader.getSystemResource("cn/jq/jqresource/pojo/User.class").getPath();
        String d2 = ClassLoader.getSystemResource("static/a.txt").getPath();
        String d3 = ClassLoader.getSystemResource("static/resource/jq.docx").getPath();

        // 5、通过Thread方式的ClassLoader的getResource方法
        String e1 = Thread.currentThread().getContextClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();
        String e2 = Thread.currentThread().getContextClassLoader().getResource("static/a.txt").getPath();
        String e3 = Thread.currentThread().getContextClassLoader().getResource("static/resource/jq.docx").getPath();
    }

二、resources下创建临时文件

    public void createFile(HttpServletRequest request) throws IOException {
        String contextPath = request.getContextPath(); // /JQ_Resource

        String filePath = contextPath + "/temp/hr.zip";
        String dirPath = contextPath + "/temp/hr";
        File file = new File(filePath);
        File dir = new File(dirPath);
        if (file.exists()) {
            // 删除指定文件,不存在报异常
            FileUtils.forceDelete(file);
        }
        file.createNewFile();


        if (dir.isDirectory()) {
            // 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错
            FileUtils.cleanDirectory(dir);
        } else {
            dir.mkdirs();
        }

        File dir_File = new File(dirPath + "/" + "dir_file.txt");

        System.out.println(dir_File.getPath()); // \JQ_Resource\temp\hr\dir_file.txt
        System.out.println(file.exists()); // true
    }

知识点回顾:

    Servlet的继承体系与HttpServletRequset和HttpServletResponse

    FileUtils工具类常用方法

  

ends~

    原文作者:Charge8
    原文地址: https://blog.csdn.net/qq_42402854/article/details/104598882
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞