这篇文章主要介绍了SpringBoot jar启动下如何读取文件路径,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
SpringBoot jar启动下读取文件路径
由于我们经常使用jar 包作为我们的项目启动方式 以及我们经常会设涉及到生成文件这时候就需要一个文件路劲存放临时文件 因为我们正在存放可以在第三方服务器或者自己文件服务器。
下面就介绍一种jar 下生成文件存放示例。
代码如下
@GetMapping("/index")
public String getFile() throws IOException {
try {
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) {
path = new File("");
System.err.println("path" + path.getAbsolutePath());
}
File upload = new File(path.getAbsolutePath(), "static/temp/");
if (!upload.exists()) {
boolean mkdirs = upload.mkdirs();
String text = "drj测试";
FileOutputStream fos = new FileOutputStream(upload.getAbsolutePath() +File.separator+ "drj.txt");
fos.write(text.getBytes());
fos.close();
System.err.println("不存在" + mkdirs);
} else {
System.err.println(upload.getAbsolutePath());
System.err.println("存在");
}
return "success";
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "error";
}
截图如下




最后处理完业务逻辑 上传到自己服务器 后删除临时文件
SpringBoot获取路径的方式
前置条件
http://127.0.0.1:9001/aiforce/authentication/sso

1)request.getContextPath()
/aiforce
2)request.getServletPath()
/authentication/sso
只返回传递到servlet的路径
3)request.getPathInfo()
/authentication/sso
只返回传递到servlet的路径
4)request.getRequestURI
/aiforce/authentication/sso
5)request.getRequestURL
http://localhost:9001/aiforce/authentication/sso
返回完整路径
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程学习网。
本文标题为:SpringBoot中jar启动下如何读取文件路径
基础教程推荐
- Java+mysql实现学籍管理系统 2023-03-16
- springboot下使用shiro自定义filter的个人经验分享 2024-02-27
- 深入理解约瑟夫环的数学优化方法 2024-03-07
- Java中EnvironmentAware 接口的作用 2023-01-23
- JSP 动态树的实现 2023-12-17
- 使用Java和WebSocket实现网页聊天室实例代码 2024-02-25
- 运用El表达式截取字符串/获取list的长度实例 2023-08-01
- Java编写实现窗体程序显示日历 2023-01-02
- JavaWeb 实现验证码功能(demo) 2024-04-14
- 是否适合从javabean类更新数据库? 2023-11-04
