`
喜欢蓝色的我
  • 浏览: 360725 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

通过Spring Resource接口获取资源(实例一)

    博客分类:
  • java
 
阅读更多

该方法实现读取本地文件--》解析文件(调用其他方法)-》操作完改数据库状态

public static void analysis(String test_name,int do_number){
        //  String test_name,test_result;
        //  int do_number;
          String result;
          int id;
         
     
          try {   
                //register driver and connect the database
                Class.forName("com.mysql.jdbc.Driver");
                try {
                    String url = String
                            .format("jdbc:mysql://%s/%s", host, database);
                    con = DriverManager.getConnection(url, username, pwd);
                    statement = con.createStatement();
                    statement1=con.createStatement();
                    statement2=con.createStatement();
                   
   
                   
                    FileWriter fw=null;
                    rs = statement.executeQuery("SELECT id FROM service_testcase WHERE test_name ='"+test_name+"'");
                    if(rs.next()){
                        try{
                            id = rs.getInt("id");//1 id
                            System.out.println("id,testcase_id is "+id);
                            rs1 = statement.executeQuery("SELECT result FROM service_jmetertask WHERE testcase_id ='"+id+"' and do_number ='"+do_number+"' and status = 3");//通过id+do_number+status的到result
                           
                            if(rs1.next()){
                                try{
                                    result = rs1.getString("result");
                                    //将结果写入jtl文件
               
                           
                            //anlyaze  xml file
                            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

/*  PathMatchingResourcePatternResolver是一个通配符的Resource查找器,包括:
/WEB-INF/*-context.xml
com/mycompany/**/applicationContext.xml
file:C:/some/path/*-context.xml
classpath:com/mycompany/**/applicationContext.xml

类文件等*/   
                            Resource[] resources = resolver.getResources("file:"+result);

-----------------------------------------------------------------------------------------------------------------------------------------

java代码:
查看复制到剪贴板打印 @Test  public void testClasspathAsteriskPrefixLimit() throws IOException {      ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();      //将首先通过ClassLoader.getResources("")加载目录,      //将只返回文件系统的类路径不返回jar的跟路径 
    //然后进行遍历模式匹配      Resource[] resources = resolver.getResources("classpath*:asm-*.txt");      Assert.assertTrue(resources.length == 0);      //将通过ClassLoader.getResources("asm-license.txt")加载      //asm-license.txt存在于com.springsource.net.sf.cglib-2.2.0.jar      resources = resolver.getResources("classpath*:asm-license.txt");      Assert.assertTrue(resources.length > 0);           //将只加载文件系统类路径匹配的Resource      resources = resolver.getResources("classpath*:LICENS*");      Assert.assertTrue(resources.length == 1);  } 
对于“resolver.getResources("classpath*:asm-*.txt");”,由于在项目“resources”目录下没有 所以应该返回0个资源;“resolver.getResources("classpath*:asm-license.txt");”将返回jar包 里的Resource;“resolver.getResources("classpath*:LICENS*");”,因为将只返回文件系统类路径资 源,所以返回1个资源。
       因此在通过前缀“classpath*”加载通配符路径时,必须包含一个根目录才能保证加载的资源是所有的,而不是部分。

三、“file”:加载一个或多个文件系统中的Resource。如“file:D:/*.txt”将返回D盘下的所有txt文件;

-----------------------------------------------------------------------------------------------------------------------------------------------
                            if(resources.length == 0) {
           
                                throw new IllegalArgumentException("Property source not set correctly, no JMeter Result XML file found matching "+result);

                              }

                              //get first file for now.
                              //TODO: what to do if there are more files matching the pattern?
                              File resource = resources[0].getFile();

//getFile():返回当前资源对应的File。如果当前资源不能以绝对路径解析为一个File则会抛出异常

                              Reader data;
                              if(resource.getName().endsWith(".gz")) {
                                data = new InputStreamReader(new GZIPInputStream(new FileInputStream(resource)));
                              }
                              else {
                                data = new FileReader(resource);
                              }

                              try {
                                new parser().analyze(data,id,test_name,do_number,username, pwd, host, database);//call the analyze() to get the results
                              
                              }
                              finally {
                                data.close();
                              }
                             
                              //更改service_jmetertask的status状态
                       
                              String sql="UPDATE service_jmetertask SET status= 4 WHERE testcase_id = '"+id+"'";
                              try {
                                    statement1.executeUpdate(sql);//use new connection to execute new sql
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                    e.printStackTrace();
                                    log.warn(e);
                                }
                        }catch(Exception e){
                            System.out.println("数据库读取错误rs");
                            e.printStackTrace();
                            log.warn(e);
                            }
                    }
                        }catch(Exception e){
                    System.out.println("数据库读取错误rs1");
                    e.printStackTrace();
                    log.warn(e);
                    }
                    }
                }catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
          }catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                    if (statement != null) {
                        try {
                            statement.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (con != null) {
                        try {
                            con.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
      }
          }
     
     
    

分享到:
评论

相关推荐

    Spring 2.0 开发参考手册

    5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...

    Spring-Reference_zh_CN(Spring中文参考手册)

    5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor ...

    Spring中文帮助文档

    4.2. Resource接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPathResource 4.3.3. FileSystemResource 4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource...

    spring chm文档

    5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到错误信息 5.4. Bean处理和BeanWrapper 5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 6. 使用Spring进行面向切面编程(AOP...

    Spring API

    4.2. Resource接口 4.3. 内置 Resource 实现 4.3.1. UrlResource 4.3.2. ClassPathResource 4.3.3. FileSystemResource 4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource...

    Spring攻略(第二版 中文高清版).part2

    10.4 通过BlazeDS/Spring暴露服务 411 10.4.1 问题 411 10.4.2 解决方案 411 10.4.3 工作原理 411 10.5 使用服务器端对象 418 10.5.1 问题 418 10.5.2 解决方案 418 10.5.3 工作原理 418 10.6 使用...

    Spring攻略(第二版 中文高清版).part1

    10.4 通过BlazeDS/Spring暴露服务 411 10.4.1 问题 411 10.4.2 解决方案 411 10.4.3 工作原理 411 10.5 使用服务器端对象 418 10.5.1 问题 418 10.5.2 解决方案 418 10.5.3 工作原理 418 10.6 使用...

    Spring.html

    ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...

    springmybatis

    如果不一致就会出错,这一章主要在上一讲基于接口编程的基础上完成如下事情: 1. 用 mybatis 查询数据,包括列表 2. 用 mybatis 增加数据 3. 用 mybatis 更新数据. 4. 用 mybatis 删除数据. 查询数据,前面已经讲过...

    spring-common:[已弃用]与Spring Boot应用程序一起使用的通用Spring组件库和相关类

    Spring常见 我的Spring Boot应用程序中使用的通用Spring组件库和相关类库 介绍 ... 查询以获取List功能接口 用法 资源阅读器 package foo; import net.kemitix.spring.common.ResourceReader; @Compon

    乐优商城.xmind

    GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源 BUG 分类不能打开,当添加后却能打开。 修改一天的BUG 最后发现是实体类里属性大小写的问题引起。 注意 Bule_bird 就必须写成 ...

    J2EE应用开发详解

    29 3.2.2 Class.forName()加载类的实例 30 3.2.3 loadClass获得类的实例 31 3.3 操作类的字段 31 3.3.1 获取对象的属性 31 3.4 操作类的方法 34 3.4.1 运行时调用对象的方法 34 3.4.2 无参构造函数 36 3.4.3 带参...

    客户关系管理系统框架搭建(二)

    * spring需要的jar包 * struts2需要的jar包 * jstl 需要的jar包 * junit需要的jar包 * 包的介绍 * cn.itcast.crm.container:重新封装spring容器 * cn.itcast.crm.dao:放置的是dao接口 * ...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    而要实现这一点,应通过系统的开放性来完成,既系统应是一个开放系统,只要符合一定的规范,可以简单的加入和减少系统的模块,配置系统的硬件。通过软件的修补、替换完成系统的升级和更新换代。 3、 系统的易用性和...

    cms后台管理

    //获取内容列表,可以通过此处进行更改,获取自己数据库中的数据 List<Content> list = getList(params, env); Map, TemplateModel> paramWrap = new HashMap, TemplateModel>( params); //OUT_LIST值为tag_...

    ibatis 开发指南(pdf)

    ibatis 实例配置 一个典型的配置文件如下(具体配置项目的含义见后): <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS....

    IBM portlet开发指南

    BaseURL 接口 ................................................................................................................. 57 包含Portlet Mode和 Window State信息 .....................................

Global site tag (gtag.js) - Google Analytics