服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Springboot工具类FileCopyUtils使用教程

Springboot工具类FileCopyUtils使用教程

2023-06-05 14:27凡夫贩夫 Java教程

这篇文章主要介绍了Springboot内置的工具类之FileCopyUtils的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

前言

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(),用于判断资源是否存在;

?
1
2
3
4
5
6
7
8
9
10
@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对象;

?
1
2
3
4
5
6
7
8
9
@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(路径);

?
1
2
3
4
5
6
7
8
9
10
11
@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);
}
?
1
2
3
4
5
6
7
8
9
@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):

?
1
2
3
4
5
6
7
8
9
10
@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使用教程

?
1
2
3
4
5
6
7
8
9
@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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/fox9916/article/details/128334822

 

延伸 · 阅读

精彩推荐