基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件
一、簡(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)文章希望大家以后多多支持腳本之家!
- SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能
- SpringBoot實(shí)現(xiàn)海量數(shù)據(jù)高效實(shí)時(shí)搜索功能
- SpringBoot+Elasticsearch實(shí)現(xiàn)數(shù)據(jù)搜索的方法詳解
- springboot結(jié)合redis實(shí)現(xiàn)搜索欄熱搜功能及文字過(guò)濾
- 詳解Elastic Search搜索引擎在SpringBoot中的實(shí)踐
- Spring Boot整合Elasticsearch實(shí)現(xiàn)全文搜索引擎案例解析
- Spring Boot集成ElasticSearch實(shí)現(xiàn)搜索引擎的示例
相關(guān)文章
Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Java多文件以ZIP壓縮包導(dǎo)出的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
在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)
這篇文章主要為大家詳細(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)步驟
在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ā)送郵件并攜帶附件
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)發(fā)送郵件并攜帶附件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
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)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
詳解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

