spring加载资源

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值