Spring内置的工具类里,最喜欢用的就是文件读写这一部分,虽然原生的写法也没几句,但是就是懒,不想循环、判断什么的,直接调用现成的静态方法,多高效,哈哈,这就是懒人必备。
Spring中主要通过org.springframework.core.io.Resource接口描述一个文件资源的位置信息,其常用的实现类有四个,分别是FileSystemResource、UrlResource、ClassPathResource、ServletContextResource。
(资料图片)
FileSystemResource描述文件资源的绝对路径,如D:\...;
UrlResource描述资源的一个网络位置,即URL资源,如如 file://... http://...;
ClassPathResource描述的类路径下的资源位置,如classpth:...;
ServletContextResource描述的Web容器上下文中的资源位置。下图这三个类关系:
在实际的业务开发中,根据操作资源时所处的场景,从实现类FileSystemResource、UrlResource、ClassPathResource、ServletContextResource中选择合适的实现类,进行相应的操作。我在项目里经常操作classpath下的自定义配置文件,下面是两个我常用的方法:
booleanexists(),用于判断资源是否存在;
@Test public void test1() throws IOException { //在与application.properties同级的resources目录下放置一张照片"zhangsan.jpeg" ClassPathResource classPathResource = new ClassPathResource("zhangsan.jpeg"); boolean exists = classPathResource.exists(); Assert.isTrue(exists, "zhangsan资源不存在"); ClassPathResource classPathResource2 = new ClassPathResource("zhangsan2.jpeg"); boolean exists2 = classPathResource2.exists(); Assert.isTrue(exists2, "zhangsan2资源不存在"); }
InputStream getInputStream(),可以从资源中获得InputStream对象;
@Test public void test2() throws IOException { //在与application.properties同级的resources目录下放置一张照片"zhangsan.jpeg" ClassPathResource classPathResource = new ClassPathResource("zhangsan.jpeg"); InputStream inputStream = classPathResource.getInputStream(); String userDir = System.getProperty("user.dir"); File file = new File(userDir + File.separator +"zhangsan2.jpeg"); FileCopyUtils.copy(FileCopyUtils.copyToByteArray(inputStream), file); }
这里要稍微拐个弯,说一个计算资源描述中两个经常傻傻分不清楚的东西:URL和URI。
URI统一资源标识符,用一个紧凑一些的字符串标标识资源,或者通俗理解为URL的父类,URL是URI的子类。
URL统一资源定位符,主要用于网络资源的访问,其中关键的属性有 protocol(通信协议)、host(主机ip)、port(端口)、path(路径);
@Test public void test4() throws IOException { //百度上随便找了一个图片的地址 URL url = new URL("https://z3.ax1x.com/2021/09/28/4fzZV0.md.jpg"); InputStream inputStream = url.openStream(); //用户当前工作目录,即当前项目的根目录, //“user.home”是用户根目录,即用户在操作系统的根目录,即C:\Users\admin String userDir = System.getProperty("user.dir"); File file = new File(userDir + File.separator + "aaa.jpg"); FileCopyUtils.copy(FileCopyUtils.copyToByteArray(inputStream), file); }
@Test public void test5() throws IOException, URISyntaxException { //百度上随便找了一个图片的地址 URI uri = new URI("https://z3.ax1x.com/2021/09/28/4fzZV0.md.jpg"); InputStream inputStream = uri.toURL().openStream(); String userDir = System.getProperty("user.dir"); File file = new File(userDir + File.separator + "aaa2.jpg"); FileCopyUtils.copy(FileCopyUtils.copyToByteArray(inputStream), file); }
前面之所以先说一下Resource,是因为要实现文件的读写,必然要对文件本身进行一些包装,即用程度代码来描述一下文件,Resource的不同实现类,其实质就是对不同场景下文件资源的更具体的描述。FileCopyUtils和StreamUtils中封装了具体读写的静态方法。
org.springframework.util.FileCopyUtils:
输入
byte[]copyToByteArray(Filein),把文件读入到字节数组中
byte[]copyToByteArray(InputStreamin),从输入流中读入到字节数组中
输出
void copy(byte[] in, File out),把字节数组写到文件中。
int copy(File in, File out),从写入文件写出到输出文件里。
void copy(byte[] in, OutputStream out),从字节数组读取到输出流。
int copy(InputStream in, OutputStream out),从输入流写出到输出流。
int copy(Reader in, Writer out),从输入流到输出流。
void copy(String in, Writer out),从字符串到输出流。
我最喜欢用的是byte[]copyToByteArray(Filein)和void copy(byte[] in, File out):
@Test public void test2() throws IOException { //在与application.properties同级的resources目录下放置一张照片"zhangsan.jpeg" ClassPathResource classPathResource = new ClassPathResource("zhangsan.jpeg"); InputStream inputStream = classPathResource.getInputStream(); String userDir = System.getProperty("user.dir"); File file = new File(userDir + File.separator +"zhangsan2.jpeg"); byte[] bytes = FileCopyUtils.copyToByteArray(inputStream); FileCopyUtils.copy(bytes, file); }
org.springframework.util.StreamUtils,和FileCopyUtils差不多,有点不太明白,为什么封装了两个?有人知道原因的,评论区告诉我呗,一块学习一下。
@Test public void test6() throws IOException { //在与application.properties同级的resources目录下放置一张照片"zhangsan.jpeg" ClassPathResource classPathResource = new ClassPathResource("zhangsan.jpeg"); InputStream inputStream = classPathResource.getInputStream(); String userDir = System.getProperty("user.dir"); FileOutputStream fileOutputStream = new FileOutputStream(userDir + File.separator + "zhangsan3.jpeg"); StreamUtils.copy(inputStream, fileOutputStream); }
到此这篇关于Springboot工具类FileCopyUtils使用教程的文章就介绍到这了,更多相关Springboot FileCopyUtils内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
关键词:
Angew. Chem. :基于菲并咔唑稠环芳烃的聚合物空穴输运材料提升钙钛矿太阳能电池效率
铭利达:关于新能源汽车配套服务方面 公司正在与特斯拉保持密切的沟通中|今日热门
古代寓言故事改写300字以上-古代寓言故事改写300_每日热讯
羽毛球——泰国公开赛:何冰娇晋级半决赛-世界消息
全球要闻:新疆塔里木盆地形成、演化与油气关系研究_关于新疆塔里木盆地形成、演化与油气关系研究简介
潞安环能(601699)6月2日主力资金净买入5884.17万元|全球今亮点
环球看热讯:中关村企业家文化艺术交流会暨海淀区东升镇2023年新春系列活动圆满举办
当前关注:教师面试技巧(体育教师面试技巧)
讲好新疆故事 共建民族团结——“石榴籽 祖国行”新疆青少年文化交流项目启动 天天报资讯
bpd是胎儿的什么正常值_bpd是胎儿的什么 环球播报
何宇鑫:6.2非农前瞻数据分析以及后市建议!
甘肃省职称申报评审管理信息系统入口_甘肃省职称申报评审系统-快资讯
国家奖学金院系意见(国家奖学金推荐意见)
大气压的应用10个例子(大气压的应用)
前海人寿·Vcare关爱空间 “童”你快乐成长
“双碳”引领 盐城新能源产业闯出发展新路子
【全球报资讯】n型竞赛打响 正泰新能多维度拓宽“护城河”
动态焦点:赛为智能(300044):该股换手率大于8%(06-02)
如何把微信视频保存到电脑上(怎样永久保存大量视频)
【世界聚看点】极限工况下四轮电动轮驱动电动汽车转矩分配控制研究_关于极限工况下四轮电动轮驱动电动汽车转矩分配控制研究概略
全志科技6月2日快速反弹|天天信息
深化电信基础设施共建共享
公交507路、515路恢复运营的通告 今日聚焦
英语高考高频词汇2023_英语高考高频词汇 焦点热议
招商银行多张信用卡怎么还款 还款后额度什么时候恢复
最高人民检察院依法对纪国刚作出逮捕决定
郑州1分钱乘地铁活动2023-全球观天下
换电柜设置存在阻力 电动自行车换电有待加强顶层设计
2023年初级药师考试成绩查询入口已开通 当前速看
广发期货:钢材市场博弈可能出台宽松政策