比如,如果是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("") 项目启动后会自动在项目根目录下生成一个
Java中有多种加载资源的方式: this.getClass().getResource(resourceName) 除非以”/“开头,否则找this类同包的文件。...ClassLoader.getSystemClassLoader().getResource(resourceName) 需要使用root开始的完整路径 代码示例 项目结构: package com.logicbig.example...二、加载Maven的资源 maven项目资源放在:src/main/resources目录。...中的资源加载 this.getClass().getResource: 如果用root开始的路径如”/root-resource.txt“才生效。...ClassLoader.getSystemClassLoader().getResource(resourceName): 和上面一样,不带斜线才生效。 如何修改maven中的资源目录?
ClassLoader[]{classLoader, this.defaultClassLoader, Thread.currentThread().getContextClassLoader(), this.getClass...returnValue; } } } return null; } 6.我们进入类加载器加载资源文件的代码中,我们可以看到首先获取全路径的url,然后再调用openStream...url.openStream() : null; } catch (IOException e) { return null; } } 6.1.我们跟进getResource...()去获取stream流: public final InputStream openStream() throws java.io.IOException { return openConnection...ByteArrayInputStream(var4.toString().getBytes()); } return this.is; } 到这里,整个获取inputstream的过程已经结束,只要把返回值往上一层返回就可以得到这个配置文件所需要的
ClassLoader[]{classLoader, this.defaultClassLoader, Thread.currentThread().getContextClassLoader(), this.getClass...url.openStream() : null; } catch (IOException e) { return null; } } 6.1.我们跟进getResource...var6; } } return null; } 我们知道getBootstrapResource(name)里面主要是url(文件资源的路径),然后使用url.openStream...()去获取stream流: public final InputStream openStream() throws java.io.IOException { return openConnection...ByteArrayInputStream(var4.toString().getBytes()); } return this.is; } 到这里,整个获取inputstream的过程已经结束,只要把返回值往上一层返回就可以得到这个配置文件所需要的
" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"..." xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"...\\*/", "" ); } } this.getClass().getClassLoader().getResource("config.json").toURI().getPath(); 此处需要先转为...String physicalPath = (String) conf.get("rootPath") + savePath; //InputStream is = fileStream.openStream...别急,SpringBoot主张打包成Jar包运行,我们用Maven来打包运行试试 ? java -jar 打开项目地址,点击上传图片,发现竟然上传不了了??!! ? 这是怎么回事呢?
" 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
坑开始了,网上大多数做法是如下代码:将词库dict.txt放在resource文件夹下,然后通过this.getClass().getResource("/dict.txt")获取资源路径后,直接使用loadUserDict...接口加载词库URL file_path = this.getClass().getResource("/dict.txt");Path path = Paths.get(new File(this.getClass...(path);以上代码,在本地跑没有任何问题,但是我们在第三步打jar包就会出现’the return value of "java.lang.ClassLoader.getResource(String...4 其他问题4.1 java版本配置问题项目从mac平台移植到win后,出现java: 警告: 源发行版 9 需要目标发行版 9只要是java配置不同,修改下就好,9改为8 org.springframework.boot spring-boot-maven-plugin
1、遇到问题 (1)Maven项目开发阶段正常运行,Java程序可以读取配置文件 public class Main { public static void main(String[] args...readFile(String fileName) throws Exception { String path = SharepointApp.class.getClassLoader().getResource...项目打成jar包后,放到服务器上运行时,却报错,找不到配置文件。...所以jar包中的类源代码用File f=new File(项目内地址)的形式,是不可能定位到文件资源的。 3、解决办法 jar中资源有其专门的URL形式:jar:!...(cfgFile).openStream(); BufferedReader br=new BufferedReader(new InputStreamReader(in));
"main" java.lang.NullPointerException at cn.hadron.util.FileUtil.readConfigFile(FileUtil.java:583...晚上时突然想到了,原来我这个项目是maven web项目 resources目录下存放的资源文件经过编译后直接放到了项目根目录下了,上面语句去掉resources/即可。.../haron/workspace/elastic/target/classes/table.xml 然而之前的Maven项目为什么通过下面语句可以获取项目中资源文件呢?...,而且必须加上resources目录 FileUtil.class.getClassLoader().getResource("resources/table.xml"); 这是因为之前的Maven...项目中resources是包名,现在的Maven Web项目resources属于Source Folder。
|| arthasHome.trim().isEmpty()) { // 解压出 arthasHome URL coreJarUrl = this.getClass...().getClassLoader().getResource("arthas-bin.zip"); if (coreJarUrl !...File tempArthasDir = createTempDir(); ZipUtil.unpack(coreJarUrl.openStream...} } }} ArthasAgent的init方法先尝试加载java.arthas.SpyAPI,若SpyAPI.isInited()为true则直接返回...= null) {result.put("errorMessage", errorMessage);}return result;}}ArthasEndPoint提供了一个读方法返回arthasConfigMap
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
LOG.debug("Preparing error message for key: [#0]", errorKey); } return LocalizedTextUtil.findText(this.getClass...evaluated as such. // 故执行了http 请求头content-type 中的{}内部引入的指令 // reference:https://struts.apache.org/maven...FileItemFactory fac = this.getFileItemFactory(); if (fac == null) { throw new NullPointerException...item.isFormField(), item.getName()); try { Streams.copy(item.openStream...throws FileUploadException, IOException { if (ctx == null) { throw new NullPointerException
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
= 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
点击关注公众号,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
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
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
“/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 ${*} 此时便可以正常运行了(采用绝对路径可以正常请求的情况下,排除文件问题); 发布者:全栈程序员栈长,转载请注明出处
spring的容器中,并能够有效的通过将另一个方法名作为参数,获得对应方法的返回值。...,经常用到的方法,是为了解决相对路径的问题,可返回站点的根路径。...对于第一部署方法,request.getContextPath()的返回值为空(即:””,中间无空格,注意区分null)。 对于第二部署方法,其返回值为:/创建的文件夹的名称。...: //获取类加载的根路径(/classes) (1)this.getClass().getResource(“/”).getPath(); // 获取当前类的所在工程路径 this.getClass...().getResource(“”).getPath(); //获取类加载的根路径(/classes) (2)this.getClass().getClassLoader().getResource(
配置中这些属性都是以字符串的形式来配置的,因此需要额外处理. 5.如果属性property标签有ref属性,说明某个属性的值是一个对象,那么根据id(ref属性的值)去获取ref对应的对象,再给属性赋值. 6.返回建立的对象...,如果没有对应的id,或者下没有子标签都会返回null demo maven dom4j...} SAXReader saxReader = new SAXReader(); Document document = saxReader.read(this.getClass...().getClassLoader().getResource(xpath)); //获取到根节点 Element rootElement = document.getRootElement...().getClassLoader().getResourceAsStream(xmlPath) 获取当前项目路径xmlPath