最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

創(chuàng)建一個springboot項目

      

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

獲取resources下的文件路徑

總結(jié)起來有兩點:

1、Class.getResource()的獲取資源路徑

  • 如果以 / 開頭,則從根路徑開始搜索資源。
  • 如果不以 / 開頭,則從當(dāng)前類所在的路徑開始搜索資源。

2、ClassLoader.getResource()的資源獲取不能以 / 開頭,統(tǒng)一從根路徑開始搜索資源。

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”)
  • 構(gòu)造:File(path)
  • 構(gòu)造:FileInputStream(“path”)
XXX.class.getResource("").getPath()

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

(以下演示均為Windows系統(tǒng))

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

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

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

表示當(dāng)前用戶目錄,即jvm調(diào)用目錄

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練  習(xí)
        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練  習(xí)/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練  習(xí)/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練  習(xí)/bin/
            return p;  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new RuntimeException(e);  
        }  
    }  
}  

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

總結(jié)

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

相關(guān)文章

  • 快速了解Hibernate中的Session

    快速了解Hibernate中的Session

    這篇文章主要介紹了快速了解Hibernate中的Session,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • 在同一個類中調(diào)用帶有@Transactional注解的方法示例

    在同一個類中調(diào)用帶有@Transactional注解的方法示例

    這篇文章主要為大家介紹了在同一個類中調(diào)用帶有@Transactional注解的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Java利用Optional解決空指針異常

    Java利用Optional解決空指針異常

    這篇文章主要介紹了Java利用Optional解決空指針異常,Optional?類是一個包含有可選值的包裝類,這意味著?Optional?類既可以含有對象也可以為空
    2022-09-09
  • 一文掌握IDEA中的Maven集成與創(chuàng)建

    一文掌握IDEA中的Maven集成與創(chuàng)建

    maven是用來幫助我們快速搭建項目結(jié)構(gòu)與開發(fā)環(huán)境的好工具,這篇文章主要介紹了一文掌握IDEA中的Maven集成與創(chuàng)建,需要的朋友可以參考下
    2023-02-02
  • Java多線程編程之讀寫鎖ReadWriteLock用法實例

    Java多線程編程之讀寫鎖ReadWriteLock用法實例

    這篇文章主要介紹了Java多線程編程之讀寫鎖ReadWriteLock用法實例,本文直接給出編碼實例,需要的朋友可以參考下
    2015-05-05
  • 基于springmvc之常用注解,操作傳入?yún)?shù)

    基于springmvc之常用注解,操作傳入?yún)?shù)

    這篇文章主要介紹了springmvc之常用注解,操作傳入?yún)?shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解SpringBoot的事務(wù)管理

    詳解SpringBoot的事務(wù)管理

    Springboot內(nèi)部提供的事務(wù)管理器是根據(jù)autoconfigure來進(jìn)行決定的。接下來通過本文給大家介紹SpringBoot的事務(wù)管理相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧
    2017-04-04
  • Java讀寫.properties文件解決中文亂碼問題

    Java讀寫.properties文件解決中文亂碼問題

    這篇文章主要介紹了Java讀寫.properties文件解決中文亂碼問題,非常具有實用價值,需要的朋友可以參考下
    2017-11-11
  • java算法題解Leetcode763劃分字母區(qū)間示例

    java算法題解Leetcode763劃分字母區(qū)間示例

    這篇文章主要為大家介紹了java算法題解Leetcode763劃分字母區(qū)間示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • java 字符串的拼接的實現(xiàn)實例

    java 字符串的拼接的實現(xiàn)實例

    這篇文章主要介紹了java 字符串的拼接的實現(xiàn)實例的相關(guān)資料,希望通過本文大家能掌握字符拼接的實現(xiàn),需要的朋友可以參考下
    2017-09-09

最新評論

商城县| 洪湖市| 江陵县| 岳阳市| 烟台市| 射阳县| 教育| 金山区| 潮安县| 阿鲁科尔沁旗| 修武县| 灵山县| 高邑县| 锡林浩特市| 共和县| 信宜市| 邵阳县| 四川省| 和田县| 海口市| 信宜市| 开封县| 沧州市| 鄂州市| 沈阳市| 黑山县| 思南县| 邢台县| 龙岩市| 枞阳县| 石台县| 蓝山县| 宽城| 读书| 资溪县| 尤溪县| 宜川县| 东莞市| 理塘县| 肥乡县| 宣武区|