Java 獲取resources下的文件路徑和創建臨時文件

       之前處理根據模板文件,批量導入xxx.zip 的下載功能,用到這兩個知識,就簡單記錄下,對於流的處理就跳過了      

由於maven項目打包會把 src/main/java 和 src/main/resources 下的文件放到 target/classes 下,所以統一以根路徑代表此目錄。

創建一個springboot項目

      

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~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章