基于Spring实现搜索目录下指定名称文件

 更新时间:2025年08月03日 10:16:01   作者:爱码少年 00fly.online  
这篇文章主要为大家详细介绍了如何基于Spring实现搜索目录下指定名称文件,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

一、简单需求

需要在指定目录下搜索指定名称文件列表

一般思路是通过递归遍历文件,然后通过过滤的方法去实现,

spring的PathMatchingResourcePatternResolver 给了我们另外一种简单实现。

二、源码实现

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内文件
        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);
    }
}

三、运行结果

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]
。。。。。。

到此这篇关于基于Spring实现搜索目录下指定名称文件的文章就介绍到这了,更多相关Spring文件搜索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot之groups应对不同的Validation规则自定义方式

    SpringBoot之groups应对不同的Validation规则自定义方式

    这篇文章主要介绍了SpringBoot之groups应对不同的Validation规则自定义方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java实战之医院管理系统的实现

    Java实战之医院管理系统的实现

    这篇文章主要介绍了如何利用Java实现医院管理系统,文中用到的技术有:SpringBoot、Layui、Freemaker等,感兴趣的同学可以了解一下
    2022-04-04
  • Mybatis动态调用表名和字段名的解决方法

    Mybatis动态调用表名和字段名的解决方法

    今天在项目开发中有个业务是需要限制各个用户对某些表里的字段查询以及某些字段是否显示,这种情况下,就需要构建sql来动态传入表名、字段名了,下面给大家介绍mybatis动态调用表名和字段名的解决方法,一起看看吧
    2016-10-10
  • springboot使用com.github.binarywang包实现微信网页上的支付和退款

    springboot使用com.github.binarywang包实现微信网页上的支付和退款

    最近做项目需要实现在pc端需要实现微信的支付,本文主要介绍了springboot使用com.github.binarywang包实现微信网页上的支付和退款,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Mybatis-plus 双主键的实现示例

    Mybatis-plus 双主键的实现示例

    本文主要介绍了Mybatis-plus 双主键的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • Java基本语法笔记(菜鸟必看篇)

    Java基本语法笔记(菜鸟必看篇)

    下面小编就为大家带来一篇Java基本语法笔记(菜鸟必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • java实现模拟USB接口的功能

    java实现模拟USB接口的功能

    本文主要介绍了java实现模拟USB接口的功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • JAVA多线程中join()方法的使用方法

    JAVA多线程中join()方法的使用方法

    虽然关于讨论线程join()方法的博客已经非常极其特别多了,但是前几天我有一个困惑却没有能够得到详细解释,当系统中正在运行多个线程时,join()到底是暂停了哪些线程,所以本文详细解释一下希望能帮助到和我有相同困惑的同学
    2021-05-05
  • maven打包时配置多环境参数的实现

    maven打包时配置多环境参数的实现

    本文主要介绍了maven打包时配置多环境参数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-04-04
  • 手把手教你用Java给暗恋对象发送一份表白邮件

    手把手教你用Java给暗恋对象发送一份表白邮件

    随着我们学习java的深入,也渐渐发现了它的一些乐趣,比如发送邮件,下面这篇文章主要给大家介绍了关于如何利用Java给暗恋对象发送一份表白邮件的相关资料,需要的朋友可以参考下
    2021-11-11

最新评论