最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot工具類FileCopyUtils使用教程

 更新時間:2022年12月30日 16:59:51   作者:凡夫販夫  
這篇文章主要介紹了Springboot內(nèi)置的工具類之FileCopyUtils的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

前言

Spring內(nèi)置的工具類里,最喜歡用的就是文件讀寫這一部分,雖然原生的寫法也沒幾句,但是就是懶,不想循環(huán)、判斷什么的,直接調(diào)用現(xiàn)成的靜態(tài)方法,多高效,哈哈,這就是懶人必備。

Resource

Spring中主要通過org.springframework.core.io.Resource接口描述一個文件資源的位置信息,其常用的實(shí)現(xiàn)類有四個,分別是FileSystemResource、UrlResource、ClassPathResource、ServletContextResource。

FileSystemResource描述文件資源的絕對路徑,如D:\...;

UrlResource描述資源的一個網(wǎng)絡(luò)位置,即URL資源,如如 file://... http://...;

ClassPathResource描述的類路徑下的資源位置,如classpth:...;

ServletContextResource描述的Web容器上下文中的資源位置。下圖這三個類關(guān)系:

在實(shí)際的業(yè)務(wù)開發(fā)中,根據(jù)操作資源時所處的場景,從實(shí)現(xiàn)類FileSystemResource、UrlResource、ClassPathResource、ServletContextResource中選擇合適的實(shí)現(xiàn)類,進(jìn)行相應(yīng)的操作。我在項(xià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)一資源標(biāo)識符,用一個緊湊一些的字符串標(biāo)標(biāo)識資源,或者通俗理解為URL的父類,URL是URI的子類。

URL統(tǒng)一資源定位符,主要用于網(wǎng)絡(luò)資源的訪問,其中關(guān)鍵的屬性有 protocol(通信協(xié)議)、host(主機(jī)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();
    //用戶當(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 {
    //百度上隨便找了一個圖片的地址
    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)文件的讀寫,必然要對文件本身進(jìn)行一些包裝,即用程度代碼來描述一下文件,Resource的不同實(shí)現(xiàn)類,其實(shí)質(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差不多,有點(diǎn)不太明白,為什么封裝了兩個?有人知道原因的,評論區(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)文章

最新評論

封开县| 江口县| 绵竹市| 梅河口市| 宜阳县| 佛坪县| 台北市| 乃东县| 剑河县| 广东省| 溧水县| 石林| 化州市| 方城县| 朝阳区| 江达县| 赣州市| 乐业县| 昌黎县| 东乌| 福清市| 定边县| 乡城县| 肃宁县| 衡山县| 青川县| 普兰县| 社旗县| 高邮市| 万载县| 辛集市| 丰台区| 衡东县| 故城县| 万全县| 吉安市| 同德县| 易门县| 通山县| 铁岭县| 荣昌县|