spring资源访问(Resource接口)实例教程源码

spring资源访问(Resource接口)实例教程源码

概述:

  1. 主要介绍3种方式(当然不止三种,但是这三种基本能应付大多需求)
    FileSystemResource:以文件的绝对路径方式进行访问
    ClassPathResourcee:以类路径的方式访问
    ServletContextResource:web应用根目录的方式访问
  2. 主要公用方法介绍(Resource接口下的)
    getFilename() : 获得文件名称
    contentLength() : 获得文件大小
    createRelative(path) : 在资源的相对地址上创建新文件
    exists() : 是否存在
    getFile() : 获得Java提供的File 对象
    getInputStream() :  获得文件的流
  3. 与常规的对应方法
    FileSystemResource 效果类似于Java中的File
    ClassPathResource 效果类似于this.getClass().getResource(“/”).getPath();
    ServletContextResource 效果类似于request.getServletContext().getRealPath(“”);
  4. 说明
    1.虽然这些方法都有常规方法获得(这个也是肯定的),但是spring的这些方法都继承自Resource接口(更加方便)
    2.ServletContextResource 还演示了转码操作 EncodedResource类的使用(个人认为比较实用)
    3.今后会继续补充
    4.不多说了,上代码,看注释
Java代码  收藏代码
  1. package com.cxyapi.spring.resource;
  2. import java.io.File;
  3. import org.apache.commons.io.IOUtils;
  4. import org.junit.Test;
  5. import org.springframework.core.io.ClassPathResource;
  6. import org.springframework.core.io.FileSystemResource;
  7. import org.springframework.core.io.Resource;
  8. /**
  9.  * @author cxy@cxyapi.com
  10.  * spring学习笔记 - 资源访问(Resource接口)
  11.  * 说明:
  12.  * 主要介绍3种方式(当然不止三种,但是这三种基本能应付大多需求)
  13.  *   FileSystemResource:以文件的绝对路径方式进行访问
  14.  *   ClassPathResource:以类路径的方式访问
  15.  *   ServletContextResource:web应用根目录的方式访问
  16.  */
  17. public class ResourceTest
  18. {
  19.     /**
  20.      * FileSystemResource
  21.      * 文件系统资源访问:以文件系统的绝对路径方式进行访问
  22.      */
  23.     @Test
  24.     public void FileSystemResourceTest() throws Exception
  25.     {
  26.         String filePath=”D:/cxyapi/show.txt”;
  27.         FileSystemResource res1=new FileSystemResource(filePath);
  28.         if(res1.exists())
  29.         {
  30.             System.out.println(“资源的文件名:”+res1.getFilename());
  31.             System.out.println(“资源的文件大小:”+res1.contentLength());
  32.             //可以获取输入流和输出流,然后配合apache common的IOUtils 读取内容
  33.             System.out.println(“文件内容:”+IOUtils.toString(res1.getInputStream(),”GB2312″));
  34.             File f=res1.getFile();  //转换成Java的File对象
  35.         }else
  36.         {
  37.             System.out.println(“指定资源不存在”);
  38.         }
  39.     }
  40.     /**
  41.      * ClassPathResource:以类路径的方式访问
  42.      */
  43.     @Test
  44.     public void ClassPathResourceTest() throws Exception
  45.     {
  46.         Resource res=new ClassPathResource(“aop.xml”);
  47.         //对应的文件路径:E:\workspace\SpringTest\WebContent\WEB-INF\classes\aop.xml
  48.         System.out.println(“文件的物理路径:”+res.getFile().getAbsolutePath());
  49.         System.out.println(“对应的以往的实现方式:”+this.getClass().getResource(“/”).getPath());
  50.     }
  51. }

 

ServletContextResource的测试写在了jsp中,因为这个方法需要jsp内置对象application作为参数

Html代码  收藏代码
  1. <%@page import=”org.springframework.core.io.support.EncodedResource”%>
  2. <%@page import=”org.springframework.util.FileCopyUtils”%>
  3. <%@page import=”org.springframework.web.context.support.ServletContextResource”%>
  4. <%@page import=”org.springframework.core.io.Resource”%>
  5. <%@ page language=”java” contentType=”text/html; charset=UTF-8″
  6.     pageEncoding=”UTF-8″%>
  7. <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” ”http://www.w3.org/TR/html4/loose.dtd”>
  8. <html>
  9. <head>
  10. <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
  11. <title>ServletContextResource测试</title>
  12. </head>
  13. <body>
  14. <%
  15.     //文件的相对路径是Web应用的根路径 也就是 WebContent 或者是 WebRoot(超级实用)
  16.     Resource res = new ServletContextResource(application,”/configTest/cxyapi.txt”);
  17.     String fileName=res.getFilename();
  18.     //做个转码 输出文件内容
  19.     EncodedResource encRes=new EncodedResource(res,”UTF-8″);
  20.     String fileContent=FileCopyUtils.copyToString(encRes.getReader()).replaceAll(“\r\n”, ”<br/>”);
  21. %>
  22. <p>
  23.     <b>文件名称:</b><%=fileName%>
  24. </p>
  25. <p>
  26.     <b>文件内容:</b><br/>
  27.     <%=fileContent%>
  28. </p>
  29. </body>
  30. </html>
本文链接地址: spring资源访问(Resource接口)实例教程源码