这篇文章给大家介绍Spring中的工具类有哪些,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
文件资源操作
Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名、URL 地址以及资源内容的操作方法
访问文件资源
* 通过 FileSystemResource 以文件系统绝对路径的方式进行访问;
* 通过 ClassPathResource 以类路径的方式进行访问;
* 通过 ServletContextResource 以相对于 Web 应用根目录的方式进行访问。
package com.baobaotao.io; import java.io.IOException; import java.io.InputStream; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; public class FileSourceExample { public static void main(String[] args) { try { String filePath = "D:/masterSpring/chapter23/webapp/WEB-INF/classes/conf/file1.txt"; // ① 使用系统文件路径方式加载文件 Resource res1 = new FileSystemResource(filePath); // ② 使用类路径方式加载文件 Resource res2 = new ClassPathResource("conf/file1.txt"); InputStream ins1 = res1.getInputStream(); InputStream ins2 = res2.getInputStream(); System.out.println("res1:"+res1.getFilename()); System.out.println("res2:"+res2.getFilename()); } catch (IOException e) { e.printStackTrace(); } } }
本文题目:Spring中的工具类有哪些-创新互联
网站地址:https://www.cdcxhl.com/article6/csjdig.html
成都网站建设公司_创新互联,为您提供ChatGPT、定制开发、Google、关键词优化、域名注册、搜索引擎优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联