[英]How to inject a network file resource with Spring?
我怎么能告訴Spring
該資源應該是一個網絡資源,並讓它相應地自動裝配?
@Value("\\MY-MACHINE\thefile.txt")
private Resource file;
結果:
Caused by: java.io.FileNotFoundException: class path resource [MY-MACHINE\thefile.txt] cannot be resolved to URL because it does not exist
我假設你的應用程序部署在Windows上。 我沒有任何Windows工作站來測試它,但據我記得這是它的方式:
創建指向您的網絡位置的符號鏈接:
mklink /DC:\\my-machine \\\\MY-MACHINE\\
用絕對路徑引用這個符號鏈接:
@Value("file:///C:/my-machine/thefile.txt")
private Resource file;
似乎沒有人知道,我發現使用雙斜線是必要的,如:
@Value("\\\\MY-MACHINE\\thefile.txt")
private Resource file;
在某些情況下,您可能需要一個file:
語句,例如: file:\\\\\\\\MY-MACHINE\\\\thefile.txt"
這是使用classpath
的最簡單方法
import org.springframework.core.io.Resource;
@Value("classpath:<path to file>")
private Resource file;
訪問Windows共享上的文件無法直接使用。 有關如何執行此操作的詳細信息,請參閱使用java連接到Windows中的共享文件夾 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.