Springboot内置的工具类之FileCopyUtils
创始人
2024-04-24 02:51:51
0

前言

        Spring内置的工具类里,最喜欢用的就是文件读写这一部分,虽然原生的写法也没几句,但是就是懒,不想循环、判断什么的,直接调用现成的静态方法,多高效,哈哈,这就是懒人必备。

Resource

        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下的自定义配置文件,下面是两个我常用的方法:

boolean exists(),用于判断资源是否存在;

@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\adminString 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);
}

FileCopyUtils

        前面之所以先说一下Resource,是因为要实现文件的读写,必然要对文件本身进行一些包装,即用程度代码来描述一下文件,Resource的不同实现类,其实质就是对不同场景下文件资源的更具体的描述。FileCopyUtils和StreamUtils中封装了具体读写的静态方法。

org.springframework.util.FileCopyUtils:

输入

byte[] copyToByteArray(File in),把文件读入到字节数组中

byte[] copyToByteArray(InputStream in),从输入流中读入到字节数组中

输出

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(File in)和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);
}

StreamUtils

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

嗨,小伙伴,都看到这了,说明这篇文章对你很用嘛,收藏+关注,不迷路哦。

相关内容

热门资讯

【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...
数据分页展示逻辑 import java.util.Arrays;import java.util.List;impo...
Redis为什么选择单线程?R... 目录专栏导读一、Redis版本迭代二、Redis4.0之前为什么一直采用单线程?三、R...
【已解决】ERROR: Cou... 正确指令: pip install pyyaml
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
Lock 接口解读 前置知识点Synchronized synchronized 是 Java 中的关键字,...
Win7 专业版安装中文包、汉... 参考资料:http://www.metsky.com/archives/350.htm...
3 ROS1通讯编程提高(1) 3 ROS1通讯编程提高3.1 使用VS Code编译ROS13.1.1 VS Code的安装和配置...
大模型未来趋势 大模型是人工智能领域的重要发展趋势之一,未来有着广阔的应用前景和发展空间。以下是大模型未来的趋势和展...
python实战应用讲解-【n... 目录 如何在Python中计算残余的平方和 方法1:使用其Base公式 方法2:使用statsmod...
学习u-boot 需要了解的m... 一、常用函数 1. origin 函数 origin 函数的返回值就是变量来源。使用格式如下...
常用python爬虫库介绍与简... 通用 urllib -网络库(stdlib)。 requests -网络库。 grab – 网络库&...
药品批准文号查询|药融云-中国... 药品批文是国家食品药品监督管理局(NMPA)对药品的审评和批准的证明文件...
【2023-03-22】SRS... 【2023-03-22】SRS推流搭配FFmpeg实现目标检测 说明: 外侧测试使用SRS播放器测...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
初级算法-哈希表 主要记录算法和数据结构学习笔记,新的一年更上一层楼! 初级算法-哈希表...
进程间通信【Linux】 1. 进程间通信 1.1 什么是进程间通信 在 Linux 系统中,进程间通信...
【Docker】P3 Dock... Docker数据卷、宿主机与挂载数据卷的概念及作用挂载宿主机配置数据卷挂载操作示例一个容器挂载多个目...