0
我想春天應用程序能夠進入本地主機並加載一個給定的路徑進入資源加載器,因此它可以被讀取的文件服務器。下面的代碼試圖訪問存儲在我的桌面上的計算機上的有效.txt文件。春:如何使用的ResourceLoader注入所在的機器上,但不是在classpath
以下是我的資源加載器實現。
@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資源的方法如下:
false
/Users/wes/Documents/workspace/rest-services-AM/{
"/Users/wes/Desktop/wes.txt"}
wes.txt
我明白,這意味着我的資源沒有返回一個實際的文件,所以我不能使用它。任何幫助,將不勝感激。
謝謝。