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

基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件

 更新時(shí)間:2025年08月03日 10:16:01   作者:愛(ài)碼少年 00fly.online  
這篇文章主要為大家詳細(xì)介紹了如何基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、簡(jiǎn)單需求

需要在指定目錄下搜索指定名稱文件列表

一般思路是通過(guò)遞歸遍歷文件,然后通過(guò)過(guò)濾的方法去實(shí)現(xiàn),

spring的PathMatchingResourcePatternResolver 給了我們另外一種簡(jiǎn)單實(shí)現(xiàn)。

二、源碼實(shí)現(xiàn)

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FileListBySpring
{
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    
    String userProfile = "file:" + System.getenv().get("USERPROFILE");
    
    /**
     * 遍歷文件
     * 
     * @throws IOException
     */
    @Test
    public void testList()
        throws IOException
    {
        // 外部文件
        Resource[] jpgs = resolver.getResources(userProfile + "/Pictures/**/*.jpg");
        Arrays.stream(jpgs).forEach(System.out::println);
        
        // 外部文件
        Resource[] pngs = resolver.getResources(userProfile + "/Pictures/**/*.png");
        Arrays.stream(pngs).forEach(System.out::println);
        
        // 工程或jar內(nèi)文件
        Resource[] xmls = resolver.getResources("classpath*:**/*.xml");
        Arrays.stream(xmls).forEach(System.out::println);
        
        // 合并
        log.info("################################################## 合并后 ##################################################");
        Stream.of(jpgs, pngs, xmls).flatMap(Arrays::stream).forEach(System.out::println);
    }
    
    /**
     * 遍歷文件,支持指定文件名搜索
     * 
     * @throws IOException
     */
    @Test
    public void testList2()
        throws IOException
    {
        Resource[] pngs = resolver.getResources(userProfile + "/Pictures/**/001.png");
        Arrays.stream(pngs).forEach(System.out::println);
        
        Resource[] pngs2 = resolver.getResources(userProfile + "/Pictures/**/00*.PNG");
        Arrays.stream(pngs2).forEach(System.out::println);
        
        Resource[] xmls = resolver.getResources("file:C:/Gitee/00fly/effict-side/**/pom.xml");
        Arrays.stream(xmls).forEach(System.out::println);
    }
}

三、運(yùn)行結(jié)果

testList2

file [C:\Users\Administrator\Pictures\001.png]
file [C:\Users\Administrator\Pictures\eclipse\001.png]

file [C:\Users\Administrator\Pictures\00006.PNG]
file [C:\Users\Administrator\Pictures\00007.PNG]
file [C:\Users\Administrator\Pictures\00011.PNG]
file [C:\Users\Administrator\Pictures\0002.PNG]
file [C:\Users\Administrator\Pictures\0003.PNG]
file [C:\Users\Administrator\Pictures\0004.PNG]
file [C:\Users\Administrator\Pictures\0005.PNG]
file [C:\Users\Administrator\Pictures\0008.PNG]
file [C:\Users\Administrator\Pictures\0010.PNG]
file [C:\Users\Administrator\Pictures\009.PNG]
file [C:\Users\Administrator\Pictures\eclipse\000.PNG]
file [C:\Users\Administrator\Pictures\eclipse\006.PNG]

file [C:\Gitee\00fly\effict-side\apidoc-image\pom.xml]
file [C:\Gitee\00fly\effict-side\auto-to-swagger\jsp-to-swagger\pom.xml]
file [C:\Gitee\00fly\effict-side\class-junit-run\java-demo\pom.xml]
file [C:\Gitee\00fly\effict-side\class-junit-run\java-junit4\pom.xml]
file [C:\Gitee\00fly\effict-side\class-junit-run\run-test-boot\pom.xml]
file [C:\Gitee\00fly\effict-side\class-junit-run\run-test-simple\pom.xml]
file [C:\Gitee\00fly\effict-side\class-junit-run\tcp-boot\pom.xml]
file [C:\Gitee\00fly\effict-side\class-junit-run\tcp-java\pom.xml]
。。。。。。

到此這篇關(guān)于基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件的文章就介紹到這了,更多相關(guān)Spring文件搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法

    Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 淺談java中Map的用法

    淺談java中Map的用法

    Map簡(jiǎn)介:將鍵映射到值的對(duì)象。一個(gè)映射不能包含重復(fù)的鍵;每個(gè)鍵最多只能映射到一個(gè)值。此接口取代 Dictionary 類,后者完全是一個(gè)抽象類,而不是一個(gè)接口。
    2015-09-09
  • 在springboot中對(duì)kafka進(jìn)行讀寫(xiě)的示例代碼

    在springboot中對(duì)kafka進(jìn)行讀寫(xiě)的示例代碼

    本篇文章主要介紹了在springboot中對(duì)kafka進(jìn)行讀寫(xiě)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • JavaSE實(shí)戰(zhàn)之酒店訂房系統(tǒng)的實(shí)現(xiàn)

    JavaSE實(shí)戰(zhàn)之酒店訂房系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了如何利用JavaSE實(shí)現(xiàn)酒店訂房系統(tǒng),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JavaSE開(kāi)發(fā)有一定的幫助,需要的可以參考一下
    2022-07-07
  • IDEA2024創(chuàng)建Web項(xiàng)目以及配置Tomcat的實(shí)現(xiàn)步驟

    IDEA2024創(chuàng)建Web項(xiàng)目以及配置Tomcat的實(shí)現(xiàn)步驟

    在Web項(xiàng)目的開(kāi)發(fā)過(guò)程中,Tomcat作為一款開(kāi)源的Servlet容器,扮演著至關(guān)重要的角色,本文將詳細(xì)闡述2024版本的idea配置Tomcat的全過(guò)程,下面就來(lái)詳細(xì)的介紹一下
    2025-10-10
  • Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • maven打包時(shí)候修改包名稱帶上git版本號(hào)和打包時(shí)間方式

    maven打包時(shí)候修改包名稱帶上git版本號(hào)和打包時(shí)間方式

    這篇文章主要介紹了maven打包時(shí)候修改包名稱帶上git版本號(hào)和打包時(shí)間方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 解決IDEA克隆代碼后在右下角沒(méi)有g(shù)it分支的問(wèn)題

    解決IDEA克隆代碼后在右下角沒(méi)有g(shù)it分支的問(wèn)題

    這篇文章主要介紹了解決IDEA克隆代碼后在右下角沒(méi)有g(shù)it分支的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • spring中actuator監(jiān)視器配置詳解

    spring中actuator監(jiān)視器配置詳解

    這篇文章主要介紹了spring中actuator監(jiān)視器配置詳解,actuator主要是完成微服務(wù)的監(jiān)控,完成監(jiān)控治理,可以查看微服務(wù)間的數(shù)據(jù)處理和調(diào)用,當(dāng)它們之間出現(xiàn)了異常,就可以快速定位到出現(xiàn)問(wèn)題的地方,需要的朋友可以參考下
    2023-09-09
  • 詳解SpringBoot中Controller接收對(duì)象列表實(shí)現(xiàn)

    詳解SpringBoot中Controller接收對(duì)象列表實(shí)現(xiàn)

    這篇文章主要介紹了詳解SpringBoot中Controller接收對(duì)象列表實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評(píng)論

灌云县| 肇州县| 志丹县| 汉中市| 恭城| 新乐市| 徐州市| 昌宁县| 全椒县| 安康市| 奉化市| 黎平县| 临湘市| 瑞昌市| 兰州市| 临泉县| 宜城市| 惠水县| 通江县| 龙陵县| 和平县| 色达县| 林口县| 乃东县| 安国市| 桐庐县| 榆树市| 平江县| 南汇区| 多伦县| 调兵山市| 三穗县| 宁明县| 青川县| 阿巴嘎旗| 璧山县| 阳谷县| 永顺县| 民权县| 汤原县| 西贡区|