spring中通过Resource去获取对应的资源,Resource继承了InputStreamSource,查看InputStreamSource源码发现只有一个InputStream getInputStream(),而Resource增加了
/**
* Determine whether this resource actually exists in physical form.
* <p>This method performs a definitive existence check, whereas the
* existence of a {@code Resource} handle only guarantees a valid
* descriptor handle.
*/
boolean exists();
/**
* Indicate whether the contents of this resource can be read via
* {@link #getInputStream()}.
* <p>Will be {@code true} for typical resource descriptors;
* note that actual content reading may still fail when attempted.
* However, a value of {@code false} is a definitive indication
* that the resource content cannot be read.
* @see #getInputStream()
*/
boolean isReadable();
/**
* Indicate whether this resource represents a handle with an open stream.
* If {@code true}, the InputStream cannot be read multiple times,
* and must be read and closed to avoid resource leaks.
* <p>Will be {@code false} for typical resource descriptors.
*/
boolean isOpen();
/**
* Return a URL handle for this resource.
* @throws IOException if the resource cannot be resolved as URL,
* i.e. if the resource is not available as descriptor
*/
URL getURL() throws IOException;
/**
* Return a URI handle for this resource.
* @throws IOException if the resource cannot be resolved as URI,
* i.e. if the resource is not available as descriptor
* @since 2.5
*/
URI getURI() throws IOException;
/**
* Return a File handle for this resource.
* @throws IOException if the resource cannot be resolved as absolute
* file path, i.e. if the resource is not available in a file system
*/
File getFile() throws IOException;
/**
* Determine the content length for this resource.
* @throws IOException if the resource cannot be resolved
* (in the file system or as some other known physical resource type)
*/
long contentLength() throws IOException;
/**
* Determine the last-modified timestamp for this resource.
* @throws IOException if the resource cannot be resolved
* (in the file system or as some other known physical resource type)
*/
long lastModified() throws IOException;
/**
* Create a resource relative to this resource.
* @param relativePath the relative path (relative to this resource)
* @return the resource handle for the relative resource
* @throws IOException if the relative resource cannot be determined
*/
Resource createRelative(String relativePath) throws IOException;
/**
* Determine a filename for this resource, i.e. typically the last
* part of the path: for example, "myfile.txt".
* <p>Returns {@code null} if this type of resource does not
* have a filename.
*/
String getFilename();
/**
* Return a description for this resource,
* to be used for error output when working with the resource.
* <p>Implementations are also encouraged to return this value
* from their {@code toString} method.
* @see Object#toString()
*/
String getDescription();
可以对资源进行判断和相关操作。
在spring中,可以针对classpath(ClassPathResource),file(FileSystemResource),URL(URLResource),InputStream(InputStreamResource),ByteArray(ByteArryResource)这些不同的资源进行解析,实例如下:
// @Test
public void testClassPathResource() throws IOException {
Resource resource = new ClassPathResource("applicationContext.xml");
InputStream stream = resource.getInputStream();
BeanFactoryTest test = new BeanFactoryTest();
test.sysStream(stream);
}
// @Test
public void testFileSystemResource() throws IOException {
Resource resource = new FileSystemResource("E:\\java文档\\test.txt");
InputStream stream = resource.getInputStream();
BeanFactoryTest test = new BeanFactoryTest();
test.sysStream(stream);
}
// @Test
public void testUrlResource() throws IOException {
Resource resource = new UrlResource(
"http://localhost:8080/TestWeb/helloworld");
InputStream stream = resource.getInputStream();
BeanFactoryTest test = new BeanFactoryTest();
test.sysStream(stream);
}
// @Test
public void testInputStreamResource() throws IOException {
Resource resource = new InputStreamResource(new FileInputStream(
"E:\\java文档\\test.txt"));
InputStream stream = resource.getInputStream();
BeanFactoryTest test = new BeanFactoryTest();
test.sysStream(stream);
}
@Test
public void testByteArrayResource() throws IOException {
Resource resource = new ByteArrayResource("hell World".getBytes());
InputStream stream = resource.getInputStream();
BeanFactoryTest test = new BeanFactoryTest();
test.sysStream(stream);
}
public static void main(String[] args) {
System.out.println(StringUtils.cleanPath("E:\\java文档\\test.txt"));
}
private void sysStream(InputStream stream) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = stream.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
System.out.println(out.toString());
}
其中
StringUtils.cleanPath("E:\\java文档\\test.txt")可以转换为<pre name="code" class="java">E:/java文档/test.txt