SpringBoot中jar启动下如何读取文件路径

网友投稿 515 2022-07-22


目录SpringBoot jar启动下读取文件路径代码如下截图如下SpringBoot获取路径的方式前置条件

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();

Sthttp://ring 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

返回完整路径


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解MybatisPlus中@TableLogic注解的使用
下一篇:springcloud如何获取网关封装的头部信息
相关文章

 发表评论

暂时没有评论,来抢沙发吧~