zoukankan      html  css  js  c++  java
  • java项目测试或者不使用request,如何获取webroot路径

    1.使用jdk中的方法,然后根据项目编译后的文件存在的位置,获取到classes目录,然后向上级查询获取
    String path = EngineTest.class.getResource("/").toURI().getPath(); // /F:/eclipse2/workspace/项目/WebRoot/WEB-INF/classes/
    String canonicalPath
    = new File(path).getParentFile().getParentFile().getCanonicalPath(); // F:eclipse2workspace项目WebRoot
    System.out.println(canonicalPath);
    // F:eclipse2workspace项目WebRoot

    WebRoot后边 这个需要自己加
    2.根据System类获取项目路径,然后在找WebRoot路径
    Strng xiangmuPath = System.getProperty("user.dir"); // F:eclipse2workspace项目
    String webrootPath = xiangmuPath + "WebRoot";
    3.springboot中有很多中,这里自己用到的一种,放到templates下的文件,可以通过这种方式获取
    String path = ClassUtils.getDefaultClassLoader().getResource("templates/pdf/templateJfinal.html").getPath();
    // /F:/eclipse2/workspace/demo/target/classes/templates/pdf/templateJfinal.html
    4.也是springboot中的
    String path2 = "/templates/pdf/templateJfinal.html";//最开始的 / 加不加都行 ClassPathResource classPathResource = new ClassPathResource(path2); System.out.println(classPathResource.getURL().getPath());
    // /F:/eclipse2/workspace/demo/target/classes/templates/pdf/templateJfinal.html
    5,ResourceUtils.getURL("uploadFile").getPath()
    输出的路径是:/Users/renjianjun/study/ideaWorkSpace/rjj-download/uploadFile
    我的uploadFile是有这个文件的,然后输出。
    只可以找到项目下的一层。再下就不行了。
    测试
     ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath()
      /Users/renjianjun/study/ideaWorkSpace/wlqq/target/classes/   

    InputStream inputStream = ResourceUtils.getURL("classpath:templates/ryExcel/ryExportExcel.xls").openStream();
    这里classpath:后不可以加 /否则找不到路径下文件
     
    6,jfinal
    
     PathKit.getWebRootPath()
    /Users/renjianjun/study/ideaWorkSpace/wlqq/

      这个用的很爽的,直接写项目下的 文件夹名称  就可以获取到对应的路径

  • 相关阅读:
    ACE admin 后台管理框架
    HTML5 respond.js 解决IE6~8的响应式布局问题
    JS/JQ常见兼容辅助插件
    DEDECMS之十 修改织梦链和文章的默认来源及作者
    怎么在网站嵌入百度搜索框
    【python进阶】深入理解系统进程1
    BugkuCTF SQL注入1
    BugkuCTF 域名解析
    BugkuCTF sql注入
    BugkuCTF web3
  • 原文地址:https://www.cnblogs.com/renjianjun/p/10417189.html
Copyright © 2011-2022 走看看