這篇文章給大家介紹Spring中的工具類(lèi)有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
文件資源操作
Spring 定義了一個(gè) org.springframework.core.io.Resource 接口,Resource 接口是為了統(tǒng)一各種類(lèi)型不同的資源而定義的,Spring 提供了若干 Resource 接口的實(shí)現(xiàn)類(lèi),這些實(shí)現(xiàn)類(lèi)可以輕松地加載不同類(lèi)型的底層資源,并提供了獲取文件名、URL 地址以及資源內(nèi)容的操作方法
訪問(wèn)文件資源
* 通過(guò) FileSystemResource 以文件系統(tǒng)絕對(duì)路徑的方式進(jìn)行訪問(wèn);
* 通過(guò) ClassPathResource 以類(lèi)路徑的方式進(jìn)行訪問(wèn);
* 通過(guò) ServletContextResource 以相對(duì)于 Web 應(yīng)用根目錄的方式進(jìn)行訪問(wèn)。
package com.baobaotao.io; import java.io.IOException; import java.io.InputStream; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; public class FileSourceExample { public static void main(String[] args) { try { String filePath = "D:/masterSpring/chapter23/webapp/WEB-INF/classes/conf/file1.txt"; // ① 使用系統(tǒng)文件路徑方式加載文件 Resource res1 = new FileSystemResource(filePath); // ② 使用類(lèi)路徑方式加載文件 Resource res2 = new ClassPathResource("conf/file1.txt"); InputStream ins1 = res1.getInputStream(); InputStream ins2 = res2.getInputStream(); System.out.println("res1:"+res1.getFilename()); System.out.println("res2:"+res2.getFilename()); } catch (IOException e) { e.printStackTrace(); } } }
本文題目:Spring中的工具類(lèi)有哪些-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.rwnh.cn/article6/csjdig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、定制開(kāi)發(fā)、Google、關(guān)鍵詞優(yōu)化、域名注冊(cè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容