[英]Is spring Resource a file or directory?
我正在使用 spring 資源 API 並使用 ResourcePatternResolver 掃描我的類路徑中的文件。
在一種情況下,掃描會拾取預先構建的 jar 中的一些目錄和文件,以及文件系統上的一些目錄和文件。
在任何一種情況下,“資源”都是文件或目錄。 如何可靠地檢測資源是否指向目錄或文件,無論是否在 jar 文件中? 在 jar 內的資源上調用getFile()
會引發異常,因此我不能像最初嘗試的那樣使用它加上isFile()
。
Spring 的Resource
接口旨在成為一個更強大的接口,用於抽象對低級資源的訪問。
它有時會包裝 File 有時不會。
它有六個內置實現: UrlResource
、 ClassPathResource
、 FileSystemResource
、 ServletContextResource
、 InputStreamResource
、 ByteArrayResource
。
您可以實現自己的資源表單。
UrlResource
包裝了java.net.URL
,並可用於訪問通常可通過 ZE6B391A8D2C4D45902A23A8B6585703 訪問的任何 Object。 如果使用http:
前綴,則資源為 URL。
ClassPathResource
表示應該從類路徑中獲取的資源。 This Resource
implementation supports resolution as java.io.File
if the class path resource resides in the file system, but not for classpath resources which reside in a jar and have not been expanded (by the servlet engine, or whatever the environment is) to文件系統。 為了解決這個問題,各種Resource
實現始終支持解析為java.net.URL
。
FileSystemResource
是java.io.File
句柄的實現。它顯然支持File
和URL
的解析。
InputStreamResource
是給定InputStream
的資源實現。 如果您需要將資源描述符保存在某處,或者如果您需要多次讀取 stream,請不要使用它。
ByteArrayResource
是給定字節數組的Resource
實現。 它為給定的字節數組創建一個 ByteArrayInputStream。
所以你不應該總是使用getFile()
,因為 Spring 的Resource
並不總是代表文件系統資源。因此,我們建議你使用getInputStream()
來訪問資源內容,因為它很可能是 function 對於所有可能的資源類型。
參考: 資源
我認為您可以通過 try catch 塊包圍檢查文件的代碼:
boolean isFile = true;
try {
resource.getFile()
...
} catch (...Exception e) {
ifFile = false
}
我有類似的要求,並通過從我的搜索模式中排除目錄來解決它。 然后對於找到的每個資源,我在路徑中查找父項,並確保在寫入文件之前已創建目錄。
在我的情況下,文件可能在文件系統中,或者在類路徑中,所以我首先檢查 URI 的方案。
盡管如果名稱中有一個點,我的搜索模式可能仍會拾取目錄,但在這種情況下最好捕獲異常 -
搜索模式 - classpath*:/**/sprout/plugins/**/*.*
示例代碼 -
private void extractClientPlugins() throws IOException {
Resource[] resourcePaths = resolver.getResourcePaths(sproutPluginSearchPattern);
Path pluginFolderPath = Paths.get(sproutHome, "./plugins/");
pluginFolderPath.toFile().mkdirs();
if (resourcePaths.length == 0) {
log.info("No Sprout client side plugins found");
}
for (Resource resource : resourcePaths) {
try {
Path destinationPath = generateDestinationPath(pluginFolderPath, resource);
File parentFolder = destinationPath.getParent().toFile();
if (!parentFolder.exists()) {
parentFolder.mkdirs();
}
destinationPath.toFile().mkdirs();
copy(resource, destinationPath.toFile());
} catch (IOException e) {
log.error("could not access resource", e);
throw e;
}
}
}
private Path generateDestinationPath(Path rootDir, Resource resource) throws IOException {
String relativePath = null;
String scheme = resource.getURI().getScheme();
if ("JAR".contains(scheme.toUpperCase())) {
String[] uriParts = resource.getURL().toString().split("!");
relativePath = trimPluginPathPrefix(uriParts[1]);
} else {
String filePath = resource.getFile().getAbsolutePath();
relativePath = trimPluginPathPrefix(filePath);
}
return Paths.get(rootDir.toString(), relativePath);
}
private String trimPluginPathPrefix(String filePath) {
String[] pathParts = filePath.split("sprout/plugins/");
if (pathParts.length != 2) {
throw new RuntimeException("The plugins must be located in a path containing '**/sprout/plugins/*'");
}
return pathParts[1];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.