Java 获取resources下的文件路径和创建临时文件

编程入门 行业动态 更新时间:2024-10-08 00:26:03

Java 获取resources下的文件<a href=https://www.elefans.com/category/jswz/34/1771438.html style=路径和创建临时文件"/>

Java 获取resources下的文件路径和创建临时文件

       之前处理根据模板文件,批量导入xxx.zip 的下载功能,用到这两个知识,就简单记录下,对于流的处理就跳过了      

由于maven项目打包会把 src/main/java 和 src/main/resources 下的文件放到 target/classes 下,所以统一以根路径代表此目录。

创建一个springboot项目

      

server:port: 80servlet:context-path: /JQ_Resource

 

一、获取resources下的文件路径

总结起来有两点:

1、Class.getResource()的获取资源路径

      - 如果以 / 开头,则从根路径开始搜索资源。

      - 如果不以 / 开头,则从当前类所在的路径开始搜索资源。

2、ClassLoader.getResource()的资源获取不能以 / 开头,统一从根路径开始搜索资源。

String path = this.getClass().getClassLoader().getResource("xxx").getPath();

测试:

    public void getResource() {//1、通过Class的getResource方法String a1 = RescourceController.class.getResource("/cn/jq/jqresource/pojo/User.class").getPath();String a2 = this.getClass().getResource("../pojo/User.class").getPath();String a3 = RescourceController.class.getResource("/static/a.txt").getPath();String a4 = this.getClass().getResource("../../../../static/a.txt").getPath();System.out.println(a1.equals(a2)); // trueSystem.out.println(a4); // /D:/JQ/workspace/JQ_Resource/target/classes/static/a.txt// 2、通过本类的ClassLoader的getResource方法String b1 = RescourceController.class.getClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();String b2 = this.getClass().getClassLoader().getResource("static/a.txt").getPath();String b3 = this.getClass().getClassLoader().getResource("static/resource/jq.docx").getPath();// 3、通过ClassLoader的getSystemResource方法String c1 = ClassLoader.getSystemClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();String c2 = ClassLoader.getSystemClassLoader().getResource("static/a.txt").getPath();String c3 = ClassLoader.getSystemClassLoader().getResource("static/resource/jq.docx").getPath();// 4、通过ClassLoader的getSystemResource方法String d1 = ClassLoader.getSystemResource("cn/jq/jqresource/pojo/User.class").getPath();String d2 = ClassLoader.getSystemResource("static/a.txt").getPath();String d3 = ClassLoader.getSystemResource("static/resource/jq.docx").getPath();// 5、通过Thread方式的ClassLoader的getResource方法String e1 = Thread.currentThread().getContextClassLoader().getResource("cn/jq/jqresource/pojo/User.class").getPath();String e2 = Thread.currentThread().getContextClassLoader().getResource("static/a.txt").getPath();String e3 = Thread.currentThread().getContextClassLoader().getResource("static/resource/jq.docx").getPath();}

二、resources下创建临时文件

    public void createFile(HttpServletRequest request) throws IOException {String contextPath = request.getContextPath(); // /JQ_ResourceString filePath = contextPath + "/temp/hr.zip";String dirPath = contextPath + "/temp/hr";File file = new File(filePath);File dir = new File(dirPath);if (file.exists()) {// 删除指定文件,不存在报异常FileUtils.forceDelete(file);}file.createNewFile();if (dir.isDirectory()) {// 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错FileUtils.cleanDirectory(dir);} else {dir.mkdirs();}File dir_File = new File(dirPath + "/" + "dir_file.txt");System.out.println(dir_File.getPath()); // \JQ_Resource\temp\hr\dir_file.txtSystem.out.println(file.exists()); // true}

知识点回顾:

    Servlet的继承体系与HttpServletRequset和HttpServletResponse

    FileUtils工具类常用方法

  

ends~

更多推荐

Java 获取resources下的文件路径和创建临时文件

本文发布于:2024-02-07 05:58:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1753763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   临时文件   文件   Java   resources

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!