2014-12-01 59 views

回答

11

使用ResourceLoaderResourcePatternUtils

class Foobar { 
    private ResourceLoader resourceLoader; 

    @Autowired 
    public Foobar(ResourceLoader resourceLoader) { 
     this.resourceLoader = resourceLoader; 
    } 

    Resource[] loadResources(String pattern) throws IOException { 
     return ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern); 
    } 
} 

,並使用它像:

Resource[] resources = foobar.loadResources("classpath*:../../dir/*.txt"); 
1

如果您在使用Spring

@Autowired 
private ApplicationContext applicationContext; 
private Resource[] resources ; 

public loadResources() { 
    try { 
     resources = applicationContext.getResources("file:C:/XYZ/*_vru_*"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 
相關問題