Springboot工具類FileCopyUtils使用教程
前言
Spring內(nèi)置的工具類里,最喜歡用的就是文件讀寫這一部分,雖然原生的寫法也沒幾句,但是就是懶,不想循環(huán)、判斷什么的,直接調(diào)用現(xiàn)成的靜態(tài)方法,多高效,哈哈,這就是懶人必備。
Resource
Spring中主要通過org.springframework.core.io.Resource接口描述一個文件資源的位置信息,其常用的實現(xiàn)類有四個,分別是FileSystemResource、UrlResource、ClassPathResource、ServletContextResource。
FileSystemResource描述文件資源的絕對路徑,如D:\...;
UrlResource描述資源的一個網(wǎng)絡(luò)位置,即URL資源,如如 file://... http://...;
ClassPathResource描述的類路徑下的資源位置,如classpth:...;
ServletContextResource描述的Web容器上下文中的資源位置。下圖這三個類關(guān)系:
在實際的業(yè)務(wù)開發(fā)中,根據(jù)操作資源時所處的場景,從實現(xiàn)類FileSystemResource、UrlResource、ClassPathResource、ServletContextResource中選擇合適的實現(xiàn)類,進行相應(yīng)的操作。我在項目里經(jīng)常操作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); }
這里要稍微拐個彎,說一個計算資源描述中兩個經(jīng)常傻傻分不清楚的東西:URL和URI。
URI統(tǒng)一資源標識符,用一個緊湊一些的字符串標標識資源,或者通俗理解為URL的父類,URL是URI的子類。
URL統(tǒng)一資源定位符,主要用于網(wǎng)絡(luò)資源的訪問,其中關(guān)鍵的屬性有 protocol(通信協(xié)議)、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”是用戶根目錄,即用戶在操作系統(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 { //百度上隨便找了一個圖片的地址 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,是因為要實現(xiàn)文件的讀寫,必然要對文件本身進行一些包裝,即用程度代碼來描述一下文件,Resource的不同實現(xiàn)類,其實質(zhì)就是對不同場景下文件資源的更具體的描述。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同級的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差不多,有點不太明白,為什么封裝了兩個?有人知道原因的,評論區(qū)告訴我唄,一塊學(xué)習(xí)一下。
@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); }
到此這篇關(guān)于Springboot工具類FileCopyUtils使用教程的文章就介紹到這了,更多相關(guān)Springboot FileCopyUtils內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實現(xiàn)給出分數(shù)數(shù)組得到對應(yīng)名次數(shù)組的方法
這篇文章主要介紹了java實現(xiàn)給出分數(shù)數(shù)組得到對應(yīng)名次數(shù)組的方法,涉及java針對數(shù)組的遍歷、排序及運算的相關(guān)技巧,需要的朋友可以參考下2015-07-07Runtime.getRuntime().exec 路徑包含空格的解決
這篇文章主要介紹了Runtime.getRuntime().exec 路徑包含空格的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11解決mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時結(jié)果返回null問題
這篇文章主要介紹了mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時結(jié)果返回null問題的解決方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06微服務(wù)SpringBoot整合Jasypt加密工具的場景分析
Jasypt是Java加密工具包,能支持對密碼的哈希加密,對文本和二進制數(shù)據(jù)的對稱加解密,還能集成SpringBoot項目對配置文件中的密鑰進行加密存儲,這篇文章主要介紹了微服務(wù)SpringBoot整合Jasypt加密工具,需要的朋友可以參考下2022-10-10java使用@Scheduled注解執(zhí)行定時任務(wù)
這篇文章主要給大家介紹了關(guān)于java使用@Scheduled注解執(zhí)行定時任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Java Swing GridBagLayout網(wǎng)格袋布局的實現(xiàn)
這篇文章主要介紹了Java Swing GridBagLayout網(wǎng)格袋布局的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12