Springboot工具類FileCopyUtils使用教程
前言
Spring內(nèi)置的工具類里,最喜歡用的就是文件讀寫這一部分,雖然原生的寫法也沒幾句,但是就是懶,不想循環(huán)、判斷什么的,直接調(diào)用現(xiàn)成的靜態(tài)方法,多高效,哈哈,這就是懶人必備。
Resource
Spring中主要通過org.springframework.core.io.Resource接口描述一個(gè)文件資源的位置信息,其常用的實(shí)現(xiàn)類有四個(gè),分別是FileSystemResource、UrlResource、ClassPathResource、ServletContextResource。
FileSystemResource描述文件資源的絕對(duì)路徑,如D:\...;
UrlResource描述資源的一個(gè)網(wǎng)絡(luò)位置,即URL資源,如如 file://... http://...;
ClassPathResource描述的類路徑下的資源位置,如classpth:...;
ServletContextResource描述的Web容器上下文中的資源位置。下圖這三個(gè)類關(guān)系:

在實(shí)際的業(yè)務(wù)開發(fā)中,根據(jù)操作資源時(shí)所處的場景,從實(shí)現(xiàn)類FileSystemResource、UrlResource、ClassPathResource、ServletContextResource中選擇合適的實(shí)現(xiàn)類,進(jìn)行相應(yīng)的操作。我在項(xiàng)目里經(jīng)常操作classpath下的自定義配置文件,下面是兩個(gè)我常用的方法:
booleanexists(),用于判斷資源是否存在;
@Test
public void test1() throws IOException {
//在與application.properties同級(jí)的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對(duì)象;
@Test
public void test2() throws IOException {
//在與application.properties同級(jí)的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);
}這里要稍微拐個(gè)彎,說一個(gè)計(jì)算資源描述中兩個(gè)經(jīng)常傻傻分不清楚的東西:URL和URI。
URI統(tǒng)一資源標(biāo)識(shí)符,用一個(gè)緊湊一些的字符串標(biāo)標(biāo)識(shí)資源,或者通俗理解為URL的父類,URL是URI的子類。
URL統(tǒng)一資源定位符,主要用于網(wǎng)絡(luò)資源的訪問,其中關(guān)鍵的屬性有 protocol(通信協(xié)議)、host(主機(jī)ip)、port(端口)、path(路徑);
@Test
public void test4() throws IOException {
//百度上隨便找了一個(gè)圖片的地址
URL url = new URL("https://z3.ax1x.com/2021/09/28/4fzZV0.md.jpg");
InputStream inputStream = url.openStream();
//用戶當(dāng)前工作目錄,即當(dāng)前項(xiàng)目的根目錄,
//“user.home”是用戶根目錄,即用戶在操作系統(tǒng)的根目錄,即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 {
//百度上隨便找了一個(gè)圖片的地址
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,是因?yàn)橐獙?shí)現(xiàn)文件的讀寫,必然要對(duì)文件本身進(jìn)行一些包裝,即用程度代碼來描述一下文件,Resource的不同實(shí)現(xiàn)類,其實(shí)質(zhì)就是對(duì)不同場景下文件資源的更具體的描述。FileCopyUtils和StreamUtils中封裝了具體讀寫的靜態(tài)方法。
org.springframework.util.FileCopyUtils:
輸入
byte[]copyToByteArray(Filein),把文件讀入到字節(jié)數(shù)組中
byte[]copyToByteArray(InputStreamin),從輸入流中讀入到字節(jié)數(shù)組中
輸出
void copy(byte[] in, File out),把字節(jié)數(shù)組寫到文件中。
int copy(File in, File out),從寫入文件寫出到輸出文件里。
void copy(byte[] in, OutputStream out),從字節(jié)數(shù)組讀取到輸出流。
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同級(jí)的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差不多,有點(diǎn)不太明白,為什么封裝了兩個(gè)?有人知道原因的,評(píng)論區(qū)告訴我唄,一塊學(xué)習(xí)一下。

@Test
public void test6() throws IOException {
//在與application.properties同級(jí)的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);
}到此這篇關(guān)于Springboot工具類FileCopyUtils使用教程的文章就介紹到這了,更多相關(guān)Springboot FileCopyUtils內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用正則表達(dá)式提取XML節(jié)點(diǎn)內(nèi)容的方法示例
這篇文章主要介紹了Java使用正則表達(dá)式提取XML節(jié)點(diǎn)內(nèi)容的方法,結(jié)合具體實(shí)例形式分析了java針對(duì)xml格式字符串的正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Java 互相關(guān)聯(lián)的實(shí)體無限遞歸問題的解決
這篇文章主要介紹了Java 互相關(guān)聯(lián)的實(shí)體無限遞歸問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
java子線程解決獲取主線程的request對(duì)象問題
這篇文章主要介紹了java子線程解決獲取主線程的request對(duì)象問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
關(guān)于Spring?Cache?緩存攔截器(?CacheInterceptor)
這篇文章主要介紹了關(guān)于Spring?Cache緩存攔截器(?CacheInterceptor),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot整合Swagger接口文檔工具的流程步驟
我們在開發(fā)接口的時(shí)候,會(huì)將接口文檔給前端的開發(fā)者進(jìn)行對(duì)接,我們可以通過Postman或者Yapi等接口管理工具進(jìn)行編寫管理,實(shí)際開發(fā)中,接口的管理確實(shí)也應(yīng)該通過專業(yè)的工具管理,本文,我們就來談?wù)勗趺丛赟pringBoot整合Swagger接口文檔工具2023-08-08
java處理異常的機(jī)制關(guān)鍵字throw和throws使用解析
這篇文章主要介紹了java處理異常的機(jī)制關(guān)鍵字throw和throws使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實(shí)現(xiàn)方法
這篇文章主要介紹了java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
95%的Java程序員人都用不好Synchronized詳解
這篇文章主要為大家介紹了95%的Java程序員人都用不好Synchronized詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

