Spring-从受登录名和密码保护的远程位置获取资源列表



在我的春季启动批处理应用程序中,我从本地目录中读取了许多xml文件:"c:/infles/";而且效果很好。

@Bean
@StepScope
public MultiResourceItemReader<Situation> multiItemReader() {
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = null;
try {
resources = patternResolver.getResources("file:c:/infiles/*.xml");
} catch (Exception e) {
logger.error("error reading files", e);
} 
return new MultiResourceItemReaderBuilder<Situation>()
.name("multiItemReader").delegate(reader())
.resources(resources)
.setStrict(false)
.build();
}

现在,文件位于可通过Http访问的服务器上。我想使用Spring的资源抽象进行访问。这是一个远程安全位置,我必须经过身份验证:login=用户名,password=密码。

我尝试以这种方式访问安全位置:

String resourceUrl = "http://username:password@10.**.**.**/infiles/*.xml";  
resources = patternResolver.getResources(resourceUrl);

但是我无法访问这些文件,并且我有这个错误:

o.s.c.i.s.PathMatchingResourcePatternResolver - Cannot search for matching files underneath URL [http://username:password@10.*.*.*/infiles/] in the file system: URL [http://username:password@10.*.*.*/infiles/] cannot be resolved to absolute file path because it does not reside in the file system: http://username:password@10.*.*.*/infiles/

谢谢你的帮助。

据我所知,您希望从远程主机读取一些XML数据,并将它们转换为代码中的Situation对象。

为了这个目的,您使用了一个抽象层来";资源";具有CCD_ 2。

问题是MultiResourceItemReader#资源被设置为一个不知道远程(http)资源的对象。它对";本地文件";访问,但无法为此访问远程服务。所以它告诉你...because it does not reside in the file system

通常情况下,对/infiles/*.xml的http请求不会有任何意义(或者服务器确实有文件列表的映射和响应)。

您将需要一个远程web服务,它为您提供文件列表,然后您可以下载该列表中的每个资源。这可能会影响Spring的web客户端,即使使用XML格式的数据:https://www.baeldung.com/spring-5-webclient

最新更新