亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件

 更新時間:2022年12月29日 14:04:05   作者:Charge8  
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

獲取resources下的文件路徑和創(chuàng)建臨時文件

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

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

創(chuàng)建一個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下創(chuàng)建臨時文件

    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
    }

Java獲取文件路徑及路徑亂碼問題

System.getProperty(“user.dir”)
  • 構造:File(path)
  • 構造:FileInputStream(“path”)
XXX.class.getResource("").getPath()

XXX.class.getClassLoader().getResource("").getPath()

(以下演示均為Windows系統)

相對路徑:src/test/resources/test.txt

絕對路徑:D:\glearning\my_opensource\somproject\src\main\resources\test\test.txt

  • “.”符號:java文件所在的當前目錄(編譯后是.class文件所在的當前目錄)
  • “…”符號:java文件所在的上一級目錄(編譯后.class文件的上一級目錄)
  • “/”符號:以/開頭的,在URL類中表示項目的根路徑(maven編譯后就是target目錄的位置)。
System.getProperty(“user.dir”)

表示當前用戶目錄,即jvm調用目錄

File(path)與FileInputStream(path)

java獲取項目路徑中文亂碼

解決方法

import java.io.UnsupportedEncodingException;  
import java.net.URI;  
import java.net.URL;  
import java.net.URLDecoder;  
  
public class Test01 {  
  
    public static void main(String[] args) {  
        getPathMethod01();  
        getPathMethod02();  
        getPathMethod03();  
        getPathMethod04();  
    }  
    private static String getPathMethod01(){  
        String p = System.getProperty("user.dir");  
        System.out.println("方法一路徑:"+p);  
        //方法一路徑:E:\test\test04練  習
        return p;  
    }  
      
    private static String getPathMethod02(){  
        URL url= Test01.class.getResource("");  
        String p = url.getPath();  
        System.out.println("方法二路徑:"+p);  
		//方法二路徑:/E:/test/test04%e7%bb%83%20%20%e4%b9%a0/bin/com/fei/
        try {  
            System.out.println("方法二解碼路徑:"+URLDecoder.decode(p, "UTF-8"));  
			//方法二解碼路徑:/E:/test/test04練  習/bin/com/fei/
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return p;  
    }  
      
    private static String getPathMethod03(){  
        URL url= Test01.class.getResource("/");  
        String p = url.getPath();  
        System.out.println("方法三路徑:"+p); 
		//方法三路徑:/E:/test/test04%e7%bb%83%20%20%e4%b9%a0/bin/
        try {  
            System.out.println("方法三解碼路徑:"+URLDecoder.decode(p, "UTF-8"));  
			//方法三解碼路徑:/E:/test/test04練  習/bin/
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return p;  
    }  
    private static String getPathMethod04(){  
        try {  
            URI uri = Test01.class.getResource("/").toURI();  
            String p = uri.getPath();  
            System.out.println("方法四路徑:"+p);  
			//方法四路徑:/E:/test/test04練  習/bin/
            return p;  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        }  
    }  
}  

通過看代碼和運行結果可以看到,用url.getPath()獲取到的路徑被utf-8編碼了,用URLDecoder.decode(p, “UTF-8”)即可解碼。

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 快速解決code唯一碼(java)的簡便方法

    快速解決code唯一碼(java)的簡便方法

    下面小編就為大家?guī)硪黄焖俳鉀Qcode唯一碼(java)的簡便方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java 詳細講解分治算法如何實現歸并排序

    Java 詳細講解分治算法如何實現歸并排序

    分治算法的基本思想是將一個規(guī)模為N的問題分解為K個規(guī)模較小的子問題,這些子問題相互獨立且與原問題性質相同。求出子問題的解,就可得到原問題的解,本篇文章我們就用分治算法來實現歸并排序
    2022-04-04
  • Dubbo實現分布式日志鏈路追蹤

    Dubbo實現分布式日志鏈路追蹤

    這篇文章主要介紹了Dubbo實現分布式日志鏈路追蹤方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java?策略模式?if-else用法實例詳解

    Java?策略模式?if-else用法實例詳解

    這篇文章主要介紹了Java?策略模式?if-else用法詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • Java 中責任鏈模式實現的三種方式

    Java 中責任鏈模式實現的三種方式

    本文重點給大家介紹java中如何編寫責任鏈模式。主要從下面3個框架中的代碼中介紹。非常不錯,需要的朋友參考下吧
    2017-09-09
  • Java中CyclicBarrier?循環(huán)屏障

    Java中CyclicBarrier?循環(huán)屏障

    這篇文章主要介紹了Java中CyclicBarrier?循環(huán)屏障,可以實現讓一組線程等待至某個狀態(tài)屏障點之后再全部同時執(zhí)行,下面文章分享CyclicBarrier循環(huán)屏障的原理,需要的小伙伴可以參考一下
    2022-05-05
  • 解決gateway報netty堆外內存溢出io.netty.util.internal.OutOfDirectMemoryError

    解決gateway報netty堆外內存溢出io.netty.util.internal.OutOfDirectMemor

    這篇文章主要介紹了解決gateway報netty堆外內存溢出io.netty.util.internal.OutOfDirectMemoryError,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Spring?Boot項目集成Knife4j接口文檔的實例代碼

    Spring?Boot項目集成Knife4j接口文檔的實例代碼

    Knife4j就相當于是swagger的升級版,對于我來說,它比swagger要好用得多<BR>,這篇文章主要介紹了Spring?Boot項目集成Knife4j接口文檔的示例代碼,需要的朋友可以參考下
    2021-12-12
  • 使用多個servlet時Spring security需要指明路由匹配策略問題

    使用多個servlet時Spring security需要指明路由匹配策略問題

    這篇文章主要介紹了使用多個servlet時Spring security需要指明路由匹配策略問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java總結篇系列:Java泛型詳解

    Java總結篇系列:Java泛型詳解

    下面小編就為大家?guī)硪黄狫ava總結篇系列:Java泛型詳解。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09

最新評論