getResource +getPath() classPath
this.getClass().getClassLoader().getResource(StringUtils.EMPTY).getPath()
String path = this.getClass().getClassLoader().getResource(fileName).getPath();
String filePath = URLDecoder.decode(path, StandardCharsets.UTF_8);
getResource +getFile()
如果是文件路径的话getFile和getPath效果是一样的如果是URL路径的话getPath是带有参数的路径。
url.getFile()=/pub/files/foobar.txt?id=123456
url.getPath()=/pub/files/foobar.txt
String path = this.getClass().getClassLoader().getResource(fileName).getFile();
String filePath = URLDecoder.decode(path, StandardCharsets.UTF_8);
JAR 包 getResourceAsStream
InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
ClassPathResource
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getInputStream();
项目所在的根路径
String rootPath = System.getProperty("user.dir");
ServletContext
ServletContext context = req.getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/classes/db.properties")
标签:包中,WEB,getClassLoader,resource,String,getResource,getPath,fileName,getClass
From: https://blog.csdn.net/qq_26594041/article/details/142307601