Java程序測(cè)試上傳Maven工程代碼示例解析
創(chuàng)建普通Maven工程
導(dǎo)入所需依賴坐標(biāo):
<dependencies> <!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java --> <dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.8.RELEASE</version> </dependency> </dependencies>
在Resource目錄中編寫FastDFS配置文件【fdfs_client.conf】
connect_timeout=30 network_timeout=60 base_path=/home/fastdfs #改為自己服務(wù)器的ip tracker_server=服務(wù)器或者虛擬IP地址:22122 log_level=info use_connection_pool = false connection_pool_max_idle_time = 3600 load_fdfs_parameters_from_tracker=false use_storage_id = false storage_ids_filename = storage_ids.conf http.tracker_server_port=80
編寫測(cè)試類:
@Test public void testSample() throws Exception{ // 上傳的文件 String filePath = "C:\\Users\\User-Dai\\Pictures\\Saved Pictures\\245099.jpg"; // FastDFS上傳需要的配置文件 String configurationFilePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath(); System.out.println(configurationFilePath); // 1、加載配置文件,配置文件中的內(nèi)容就是 tracker 服務(wù)的地址。 ClientGlobal.init(configurationFilePath); // 2、創(chuàng)建一個(gè) TrackerClient 對(duì)象。直接 new 一個(gè)。 TrackerClient trackerClient = new TrackerClient(); // 3、使用 TrackerClient 對(duì)象創(chuàng)建連接,獲得一個(gè) TrackerServer 對(duì)象。 TrackerServer trackerServer = trackerClient.getConnection(); // 4、創(chuàng)建一個(gè) StorageServer 的引用,值為 null StorageServer storageServer = null; // 5、創(chuàng)建一個(gè) StorageClient 對(duì)象,需要兩個(gè)參數(shù) TrackerServer 對(duì)象、StorageServer 的引用 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 6、使用 StorageClient 對(duì)象上傳圖片。 //擴(kuò)展名不帶“.” String[] strings = storageClient.upload_file(filePath, "jpg", null); // 7、返回?cái)?shù)組。包含組名和圖片的路徑。 for (String string : strings) { System.out.println(string); } System.out.println("上傳完成"); }
測(cè)試結(jié)果:
C:\Users\User-Dai\IdeaProjects\FastDFS\target\classes\fdfs_client.conf group1 M00/00/00/rBEAB180jHGAZ-ZDAAPzHYbtkp4809.jpg 上傳完成
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題
這篇文章主要介紹了如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Spring Boot項(xiàng)目@RestController使用重定向redirect方式
這篇文章主要介紹了Spring Boot項(xiàng)目@RestController使用重定向redirect方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Maven高級(jí)的聚合和繼承的實(shí)現(xiàn)
在軟件開發(fā)中,隨著項(xiàng)目規(guī)模的擴(kuò)大,單個(gè)模塊的開發(fā)方式逐漸轉(zhuǎn)變?yōu)槎嗄K開發(fā),這種方式帶來了項(xiàng)目管理上的挑戰(zhàn),其中最常見的問題是模塊間的依賴管理和版本控制問題,本文就來介紹一下2024-10-10淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別
本文主要介紹了淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Spring自動(dòng)裝配Bean實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring自動(dòng)裝配Bean實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Shiro實(shí)現(xiàn)session限制登錄數(shù)量踢人下線功能
這篇文章主要介紹了Shiro實(shí)現(xiàn)session限制登錄數(shù)量踢人下線,本文記錄的是shiro采用session作為登錄方案時(shí),對(duì)用戶進(jìn)行限制數(shù)量登錄,以及剔除下線,需要的朋友可以參考下2023-11-11