这篇文章主要介绍了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启动下如何读取文件路径


基础教程推荐
- jsp hibernate的分页代码第3/3页 2024-01-11
- springboot中request和response的加解密实现代码 2022-12-08
- SpringBoot 2.5.5整合轻量级的分布式日志标记追踪神器TLog的详细过程 2023-06-17
- 详解http请求中的Content-Type 2023-07-31
- SpringBoot嵌入式Web容器原理与使用介绍 2023-06-17
- 用javascript制作qq注册动态页面 2023-12-16
- java 解决Eclipse挂掉问题的方法 2024-01-10
- 关于@MapperScan包扫描的坑及解决 2023-04-16
- JSP servlet实现文件上传下载和删除 2023-07-30
- Spring MVC数据绑定方式 2023-06-30