spring资源是文件还是目录? [英] Is spring Resource a file or directory?

查看:234
本文介绍了spring资源是文件还是目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring Resource API,并使用ResourcePatternResolver扫描文件的类路径。



在一种情况下,扫描将拾取一些目录和文件在一个预构建的jar中,一些在文件系统上。



在这两种情况下,资源将是文件或目录。如何可靠地检测资源是否指向目录或文件,无论是否在jar文件中?在一个jar内的资源中调用$ code> getFile()会抛出异常,所以我不能使用加$ isFile()正如我最初尝试的那样。

解决方案

Spring的资源一个更有能力的接口,用于提取对低级资源的访问。



它有时不包含文件。



它有六个内置的工具: UrlResource ClassPathResource FileSystemResource ServletContextResource InputStreamResource ByteArrayResource / p>

您可以实现自己的资源表单。



UrlResource 包裹一个 java.net.URL ,可用于访问通常可通过URL访问的任何对象。如果您使用 http:前缀,资源是一个URL。



ClassPathResource 表示应该从类路径获得的资源。如果类路径资源驻留在文件系统中,则资源实现支持解析为 java.io.File 对于驻留在jar中并且尚未扩展(通过servlet引擎,或任何环境))到文件系统的类路径资源。为了解决这个各种资源实现始终支持解析为 java.net.URL



FileSystemResource java.io.File 句柄。它显然支持解析为文件以及 URL



InputStreamResource 是为给定的 InputStream 实现的资源。如果您需要将资源描述符保留在某处,或者您需要多次读取流,则不要使用它。



ByteArrayResource 是给定字节数组的资源实现。它为给定的字节数组创建一个ByteArrayInputStream。



所以你不应该总是使用 getFile()作为Spring的资源并不总是代表文件系统资源。因此,我们建议您使用 getInputStream()来访问资源内容,因为它可能对所有可能的资源类型起作用。



请参阅:资源


I am using the spring Resource API and using a ResourcePatternResolver to scan my classpath for files.

In one situation the scan is picking up some directories and files that are in a pre-built jar and some that are on the file system.

In either case a 'resource' will either be a file or a directory. How can I reliably detect whether a resource points to a directory or file, whether in a jar file or not? Calling getFile() on a Resource inside a jar throws an Exception so I can't use that plus isFile() as I initially tried.

解决方案

Spring’s Resource interface is meant to be a more capable interface for abstracting access to low-level resources.

It wraps File sometimes while sometimes not.

It has six built-in implements: UrlResource, ClassPathResource, FileSystemResource, ServletContextResource, InputStreamResource, ByteArrayResource.

You can implement yourself resource form.

The UrlResource wraps a java.net.URL, and may be used to access any Object that is normally accessible via a URL. If you use http: prefix ,the resource is a URL.

The ClassPathResource represents a resource which should be obtained from the classpath. 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 the filesystem. To address this the various Resource implementations always support resolution as a java.net.URL.

FileSystemResource is an implement for java.io.File handles.It obviously supports resolution as a File and as a URL.

InputStreamResource is a resource implements for a given InputStream. Do not use it if you need to keep the resource descriptor somewhere, or if you need read a stream multiple times.

ByteArrayResource is a Resource implement for a given byte array. It creates a ByteArrayInputStream for the given byte array.

So you should not always use getFile() as Spring's Resource doesn't always represent a file system resource.For this reason, we recommend that you use getInputStream() to access resource contents because it is likely to function for all possible resource types.

Refer to: Resources

这篇关于spring资源是文件还是目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆