Springboot工具类FileCopyUtils使用教程

作者:凡夫贩夫 时间:2023-10-15 18:56:36 

前言

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

Resource

Spring中主要通过org.springframework.core.io.Resource接口描述一个文件资源的位置信息,其常用的实现类有四个,分别是FileSystemResource、UrlResource、ClassPathResource、ServletContextResource。

FileSystemResource描述文件资源的绝对路径,如D:\...;

UrlResource描述资源的一个网络位置,即URL资源,如如 file://... http://...;

ClassPathResource描述的类路径下的资源位置,如classpth:...;

ServletContextResource描述的Web容器上下文中的资源位置。下图这三个类关系:

Springboot工具类FileCopyUtils使用教程

在实际的业务开发中,根据操作资源时所处的场景,从实现类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);
}

FileCopyUtils

前面之所以先说一下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);
}

StreamUtils

org.springframework.util.StreamUtils,和FileCopyUtils差不多,有点不太明白,为什么封装了两个?有人知道原因的,评论区告诉我呗,一块学习一下。

Springboot工具类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);
}

来源:https://blog.csdn.net/fox9916/article/details/128334822

标签:Springboot,工具类,FileCopyUtils
0
投稿

猜你喜欢

  • C语言中fchdir()函数和rewinddir()函数的使用详解

    2022-02-08 20:08:08
  • 计算字符串和文件MD5值的小例子

    2023-12-10 20:31:19
  • Android API开发之SMS短信服务处理和获取联系人的方法

    2021-10-23 03:22:15
  • Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)

    2023-08-24 14:53:28
  • 解决IntelliJ IDEA中鼠标拖动选择为矩形区域问题

    2022-04-03 21:08:51
  • Springboot+Mybatis-plus不使用SQL语句进行多表添加操作及问题小结

    2021-09-30 10:31:10
  • Jmeter测试必知的名词及环境搭建

    2022-11-23 19:34:07
  • java实现的冒泡排序算法示例

    2022-12-29 20:35:03
  • Kotlin 匿名类实现接口和抽象类的区别详解

    2021-09-04 07:14:58
  • 一篇文章带你入门Java基本概念

    2023-11-26 01:17:08
  • 详解Spring Boot Admin监控服务上下线邮件通知

    2023-06-16 21:53:31
  • RSA密钥--JAVA和C#的区别及联系

    2022-09-18 12:16:44
  • 微信公众号 网页授权登录及code been used解决详解

    2023-02-06 18:49:00
  • SpringBoot使用自动配置xxxAutoConfiguration

    2022-11-20 09:05:56
  • Java精确抽取网页发布时间

    2022-03-24 17:20:11
  • android 实现APP中改变头像图片的实例代码

    2021-11-02 20:39:58
  • Kotlin协程launch启动流程原理详解

    2021-10-31 15:47:22
  • C# WinForm制作异形窗体与控件的方法

    2023-11-07 11:09:28
  • Android 监听应用前/后台切换实例代码

    2021-06-05 05:25:22
  • Android实现视频播放--腾讯浏览服务(TBS)功能

    2021-09-06 20:13:10
  • asp之家 软件编程 m.aspxhome.com