springboot获取项目目录路径的方法
本文讲解"springboot获取项目目录路径的方法",希望能够解决相关问题。
目录- springboot获取项目目录路径
- springboot获取resources目录资源文件9种方式
- 方式一
- 方式二
- 方式三
- 方式四(重要)
- 方式五(重要)
- 方式六(重要)
- 方式七
- 方式八
- 方式九
- 总结
springboot获取项目目录路径
springboot部署后获取项目的路径
//获取跟目录(绝对路径)
file path = new file(resourceutils.geturl("classpath:").getpath());
if(!path.exists()) path = new file("");
system.out.println("path:"+path.getabsolutepath());
//如果上传目录为/static/images/upload/,则可以如下获取:
file upload = new file(path.getabsolutepath(),"static/images/upload/");
if(!upload.exists()) upload.mkdirs();
system.out.println("upload url:"+upload.getabsolutepath());
//在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/
//在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/springboot获取resources目录资源文件9种方式

本文中提供了九种方式获取resources目录下文件的方式。
其中打印文件的方法如下:
/**
* 根据文件路径读取文件内容
*
* @param fileinpath
* @throws ioexception
*/
public static void getfilecontent(object fileinpath) throws ioexception {
bufferedreader br = null;
if (fileinpath == null) {
return;
}
if (fileinpath instanceof string) {
br = new bufferedreader(new filereader(new file((string) fileinpath)));
} else if (fileinpath instanceof inputstream) {
br = new bufferedreader(new inputstreamreader((inputstream) fileinpath));
}
string line;
while ((line = br.readline()) != null) {
system.out.println(line);
}
br.close();
}方式一
主要核心方法是使用getresource和getpath方法,这里的getresource("")里面是空字符串
public void function1(string filename) throws ioexception {
string path = this.getclass().getclassloader().getresource("").getpath();//注意getresource("")里面是空字符串
system.out.println(path);
string filepath = path + filename;
system.out.println(filepath);
getfilecontent(filepath);
}方式二
主要核心方法是使用getresource和getpath方法,直接通过getresource(filename)方法获取文件路径,注意如果是路径中带有中文一定要使用urldecoder.decode解码。
/**
* 直接通过文件名getpath来获取路径
*
* @param filename
* @throws ioexception
*/
public void function2(string filename) throws ioexception {
string path = this.getclass().getclassloader().getresource(filename).getpath();//注意getresource("")里面是空字符串
system.out.println(path);
string filepath = urldecoder.decode(path, "utf-8");//如果路径中带有中文会被urlencoder,因此这里需要解码
system.out.println(filepath);
getfilecontent(filepath);
}方式三
直接通过文件名+getfile()来获取文件。如果是文件路径的话getfile和getpath效果是一样的,如果是url路径的话getpath是带有参数的路径。
如下所示:
url.getfile()=/pub/files/foobar.txt?id=123456 url.getpath()=/pub/files/foobar.txt
使用getfile()方式获取文件的代码如下:
/**
* 直接通过文件名+getfile()来获取
*
* @param filename
* @throws ioexception
*/
public void function3(string filename) throws ioexception {
string path = this.getclass().getclassloader().getresource(filename).getfile();//注意getresource("")里面是空字符串
system.out.println(path);
string filepath = urldecoder.decode(path, "utf-8");//如果路径中带有中文会被urlencoder,因此这里需要解码
system.out.println(filepath);
getfilecontent(filepath);
}方式四(重要)
直接使用getresourceasstream方法获取流,上面的几种方式都需要获取文件路径,但是在springboot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/**
* 直接使用getresourceasstream方法获取流
* springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
*
* @param filename
* @throws ioexception
*/
public void function4(string filename) throws ioexception {
inputstream in = this.getclass().getclassloader().getresourceasstream(filename);
getfilecontent(in);
}方式五(重要)
主要也是使用getresourceasstream方法获取流,不使用getclassloader可以使用getresourceasstream("/配置测试.txt")直接从resources根路径下获取,springboot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/**
* 直接使用getresourceasstream方法获取流
* 如果不使用getclassloader,可以使用getresourceasstream("/配置测试.txt")直接从resources根路径下获取
*
* @param filename
* @throws ioexception
*/
public void function5(string filename) throws ioexception {
inputstream in = this.getclass().getresourceasstream("/" + filename);
getfilecontent(in);
}方式六(重要)
通过classpathresource类获取文件流,springboot中所有文件都在jar包中,没有一个实际的路径,因此可以使用以下方式。
/**
* 通过classpathresource类获取,建议springboot中使用
* springboot项目中需要使用此种方法,因为jar包中没有一个实际的路径存放文件
*
* @param filename
* @throws ioexception
*/
public void function6(string filename) throws ioexception {
classpathresource classpathresource = new classpathresource(filename);
inputstream inputstream = classpathresource.getinputstream();
getfilecontent(inputstream);
}方式七
通过绝对路径获取项目中文件的位置,只是本地绝对路径,不能用于服务器获取。
/**
* 通过绝对路径获取项目中文件的位置(不能用于服务器)
* @param filename
* @throws ioexception
*/
public void function7(string filename) throws ioexception {
string rootpath = system.getproperty("user.dir");//e:\workspace\git\spring-framework-learning-example
string filepath = rootpath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+filename;
getfilecontent(filepath);
}方式八
通过new file("")获取当前的绝对路径,只是本地绝对路径,不能用于服务器获取。
/**
* 通过绝对路径获取项目中文件的位置(不能用于服务器)
* @param filename
* @throws ioexception
*/
public void function8(string filename) throws ioexception {
//参数为空
file directory = new file("");
//规范路径:getcanonicalpath() 方法返回绝对路径,会把 ..\ 、.\ 这样的符号解析掉
string rootcanonicalpath = directory.getcanonicalpath();
//绝对路径:getabsolutepath() 方法返回文件的绝对路径,如果构造的时候是全路径就直接返回全路径,如果构造时是相对路径,就返回当前目录的路径 + 构造 file 对象时的路径
string rootabsolutepath =directory.getabsolutepath();
system.out.println(rootcanonicalpath);
system.out.println(rootabsolutepath);
string filepath = rootcanonicalpath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+filename;
getfilecontent(filepath);
}方式九
主要是通过设置环境变量,将文件放在环境变量中,原理也是通过绝对路径获取。
示例中我设置了一个环境变量:test_root=e:\\workspace\\git\\spring-framework-learning-example
system.getenv("test_root");
system.getproperty("test_root")通过设置环境变量的方式,然后通过绝对路径获取文件
/**
* 通过绝对路径获取项目中文件的位置
*
* @param filename
* @throws ioexception
*/
public void function9(string filename) throws ioexception {
system.setproperty("test_root","e:\\workspace\\git\\spring-framework-learning-example");
//参数为空
string rootpath = system.getproperty("test_root");
system.out.println(rootpath);
string filepath = rootpath + "\\chapter-2-springmvc-quickstart\\src\\main\\resources\\"+filename;
getfilecontent(filepath);
}
关于 "springboot获取项目目录路径的方法" 就介绍到此。希望多多支持硕编程。


