首页
学习
活动
专区
圈层
工具
发布

maven项目中获取类路径和项目路径的写法示例

比如,如果是maven项目,classpath为"项目名/target/classes",如果是普通项目,可能是”项目名/bin”,或者”项目名/build/classes”等等。...”/”开头,就在调用getResource的字节码对象所在目录下找(同样不会递归查找子目录) 测试 System.out.println("路径一为:"+this.getClass().getResource...("").getPath()); System.out.println("路径二为:"+this.getClass().getResource("/").getPath()); 说明 this.getClass...().getClassLoader().getResource("").getPath()和this.getClass().getResource("/").getPath()得到的结果一样,均为:/D...:/workspace/meas/target/classes/ 从源码中可以看到其实在getResource方法中封装了getClassLoader().getResource("") 项目启动后会自动在项目根目录下生成一个

2.7K20
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    springboot集成ueditor富文本编辑器【需要修改ueditor源码】-和上一篇不一样

    "     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd...第一次测试成功结果.png 此时点击图片上传按钮会显示后台配置项返回格式出错,上传功能将不能正常使用! 接下来就是配置关于图片上传的步骤啦! ?...=========手动修改部分=========*/         try{             //获取classpath下的config.json路径             return this.getClass...().getClassLoader().getResource("config.json").toURI().getPath();         }catch (URISyntaxException ...physicalPath = (String) conf.get("rootPath") + savePath;                  //InputStream is = fileStream.openStream

    1.7K40

    Java获取resources目录下的资源文件方法

    Web项目开发中,经常会有一些静态资源,被放置在resources目录下,随项目打包在一起,代码中要使用的时候,通过文件读取的方式,加载并使用; 今天总结整理了九种方式获取resources目录下文件的方法...{ String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("...IOException { //参数为空 File directory = new File(""); //规范路径:getCanonicalPath() 方法返回绝对路径...String rootCanonicalPath = directory.getCanonicalPath(); //绝对路径:getAbsolutePath() 方法返回文件的绝对路径...,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径 String rootAbsolutePath =directory.getAbsolutePath

    2K10

    读取 resources 目录下文件路径的九种方式

    = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串     System.out.println...().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串     System.out.println...().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串     System.out.println... throws IOException {     //参数为空     File directory = new File("");     //规范路径:getCanonicalPath() 方法返回绝对路径...,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径     String rootAbsolutePath =directory.getAbsolutePath

    96043

    获取 resources 目录资源文件的 9 种方法,还有谁不会?!

    点击关注公众号,Java干货及时送达 项目开发中,经常会有一些静态资源,被放置在resources目录下,随项目打包在一起,代码中要使用的时候,通过文件读取的方式,加载并使用; 本文中汇总整理了九种方式获取...String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串     ...().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串     System.out.println...().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串     System.out.println...,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径     String rootAbsolutePath =directory.getAbsolutePath

    3.3K10

    九种方式,教你读取 resources 目录下的文件路径

    String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串     ...().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串     System.out.println...().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串     System.out.println... throws IOException {     //参数为空     File directory = new File("");     //规范路径:getCanonicalPath() 方法返回绝对路径...,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径     String rootAbsolutePath =directory.getAbsolutePath

    2K20

    九种方法!教你如何读取resources目录下的文件路径

    String path = this.getClass().getClassLoader().getResource("").getPath();//注意getResource("")里面是空字符串...().getClassLoader().getResource(fileName).getPath();//注意getResource("")里面是空字符串         System.out.println...().getClassLoader().getResource(fileName).getFile();//注意getResource("")里面是空字符串         System.out.println...        String rootCanonicalPath = directory.getCanonicalPath();         //绝对路径:getAbsolutePath() 方法返回文件的绝对路径...,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 File 对象时的路径         String rootAbsolutePath =directory.getAbsolutePath

    3.4K30

    java p12证书_java引用微信支付的p12证书文件

    “/home/www/ceshi/apiclient_cert.p12”; 使用决定路径时,直接引用即可:File file = new File(PATH1); 2.相对路径(类加载器加载):注意:maven...打包转码问题 文件放在项目中的resources下: 引用时: String path = “apiclient_cert.p12”; File file= new File(this.getClass...().getClassLoader().getResource(path).getFile()); 类加载编译后,resources下的文件的位置: 这个时候文件正常加载进去了,但是此时坑来了,程序运行时报错...原因:maven打包时,会对文件进行转码,重新编码后会导致证书文件不可用 解决:poom依赖中进行配置,让maven打包时过滤掉不需要转码的文件 具体如下: org.apache.maven.plugins...maven-resources-plugin UTF-8 pem pfx p12 ${*} 此时便可以正常运行了(采用绝对路径可以正常请求的情况下,排除文件问题); 发布者:全栈程序员栈长,转载请注明出处

    1.8K30
    领券