Spring:如何使用resourceLoader注入位于计算机上但不在类路径中的文件
我希望spring应用程序能够进入本地主机并将给定的路径加载到资源加载程序中,以便服务器能够读取该路径。下面的代码试图访问存储在桌面上的有效.txt文件 下面是我的资源加载器实现Spring:如何使用resourceLoader注入位于计算机上但不在类路径中的文件,spring,Spring,我希望spring应用程序能够进入本地主机并将给定的路径加载到资源加载程序中,以便服务器能够读取该路径。下面的代码试图访问存储在桌面上的有效.txt文件 下面是我的资源加载器实现 @Component @Scope("prototype") public class CustomResourceLoader implements ResourceLoaderAware { @Autowired private ResourceLoader rsld; @Override
@Component
@Scope("prototype")
public class CustomResourceLoader implements ResourceLoaderAware {
@Autowired
private ResourceLoader rsld;
@Override
public void setResourceLoader(ResourceLoader rsld) {
// TODO Auto-generated method stub
this.rs = rs;
}
public void showResource(String path) throws IOException{
Resource resour = rsld.getResource("file:"+path);
File fl = resour.getFile();
System.out.println(fl.exists());
System.out.println(fl.getAbsoluteFile());
System.out.println(fl.getName());
}
}
调用show resource方法的输出如下所示:
false
/Users/wes/Documents/workspace/rest-services-AM/{
"/Users/wes/Desktop/wes.txt"}
wes.txt
我理解这意味着我的资源没有返回实际文件,因此我无法使用它。任何帮助都将不胜感激
谢谢。似乎没有人知道,我发现有必要使用双斜杠,如:
@Value("\\\\MY-MACHINE\\thefile.txt")
private Resource file;
在某些情况下,您可能需要一个file:语句,例如:file:\\MY-MACHINE\thefile.txt“