FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
在上篇文章給大家介紹了FastDFS安裝和配置整合Nginx-1.13.3的方法,大家可以點(diǎn)擊查看下。
今天使用Java代碼實(shí)現(xiàn)文件的上傳和下載。對(duì)此作者提供了Java API支持,下載fastdfs-client-java將源碼添加到項(xiàng)目中?;蛘咴贛aven項(xiàng)目pom.xml文件中添加依賴(lài)
<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27-SNAPSHOT</version> </dependency>
一 : 添加配置文件
當(dāng)完成以上操作之后可以添加鏈接FastDFS服務(wù)器的配置文件 fastdfs-client.properties
fastdfs.connect_timeout_in_seconds = 5 fastdfs.network_timeout_in_seconds = 30 fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false fastdfs.http_secret_key = FastDFS1234567890 fastdfs.http_tracker_http_port = 80 fastdfs.tracker_servers = 192.168.1.7:22122
因?yàn)轫?xiàng)目中只使用properties 格式文件,如果需要更多的配置信息,可以查看官方的配置信息
注: 1. 其他的配置項(xiàng)為可選,fastdfs.tracker_servers 為必須選項(xiàng)
2. 多個(gè)tracker_servers可以使用逗號(hào)“ , ”分隔
二 : 加載配置文件
1. 測(cè)試加載配置文件
@Test public void initConifg() throws Exception { // 加載配置文件 ClientGlobal.initByProperties("config/fastdfs-client.properties"); System.out.println("ClientGlobal.configInfo():" + ClientGlobal.configInfo()); }
2. 輸出結(jié)果
ClientGlobal.configInfo():{ g_connect_timeout(ms) = 5000 g_network_timeout(ms) = 30000 g_charset = UTF-8 g_anti_steal_token = false g_secret_key = FastDFS1234567890 g_tracker_http_port = 80 trackerServers = 192.168.1.7:22122 }
當(dāng)出現(xiàn)上面和配置文件一致的輸出結(jié)果時(shí)候,說(shuō)明加載配置文件已經(jīng)成功。
三:功能實(shí)現(xiàn)
由于是使用Junit做測(cè)試,為了方便在開(kāi)始執(zhí)行之前,初始化配置文件和獲取連接,同時(shí)沒(méi)有捕獲異常全部拋出
1.初始化連接信息
//成員變量 TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient storageClient = null; /** * 初始化連接信息 * @author: wrh45 * @date: 2017年8月5日下午8:08:57 */ @Before public void init() throws Exception { // 加載配置文件 ClientGlobal.initByProperties("config/fastdfs-client.properties"); // 獲取連接 TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageClient = new StorageClient(trackerServer, storageServer); }
注: 如果出現(xiàn)連接超時(shí)異常:java.net.SocketTimeoutException: connect timed out
1 .查看服務(wù)器地址和端口是否正確
2 .請(qǐng)查看服務(wù)器TrackerServer和StorageServer服務(wù)端口是否開(kāi)啟。默認(rèn)為22122和23000
2.上傳文件
/** * 上傳圖片 * @throws Exception * @author: wrh45 * @date: 2017年8月5日下午7:09:23 */ @Test public void uploadFileOfByte() throws Exception { // 獲取文件字節(jié)信息 file = new File("src/test/resources/pic/Ace.jpg"); FileInputStream inputStream = new FileInputStream(file); byte[] file_buff = new byte[(int) file.length()]; inputStream.read(file_buff); // 獲取文件擴(kuò)展名 String fileName = file.getName(); String extName = null; if (fileName.contains(".")) { extName = fileName.substring(fileName.lastIndexOf(".") + 1); } else { return; } // 圖片元數(shù)據(jù),如果設(shè)置為空,那么服務(wù)器上不會(huì)生成-m的原數(shù)據(jù)文件 NameValuePair[] meta_list = new NameValuePair[2]; meta_list[0] = new NameValuePair("fileName", "測(cè)試專(zhuān)用"); meta_list[1] = new NameValuePair("length", "測(cè)試專(zhuān)用"); // 文件上傳,返回組名和訪(fǎng)問(wèn)地址 String[] upload_file = storageClient.upload_file(file_buff, extName, meta_list); System.out.println(Arrays.asList(upload_file)); }
下面是執(zhí)行結(jié)果
已經(jīng)將圖片上傳到服務(wù)器,同時(shí)返回了圖片地址。那么通過(guò)這個(gè)地址試試能否訪(fǎng)問(wèn)
OK,在瀏覽器輸入地址后,成功返回了圖片信息
3.下載文件
這里通過(guò)storageClient下載文件,然后將數(shù)據(jù)存儲(chǔ)到本地。如果通過(guò)瀏覽器下載,同理將數(shù)據(jù)寫(xiě)入文件即可
/** * 下載圖片 * @throws Exception * @author: wrh45 * @date: 2017年8月5日下午8:09:10 */ @Test public void downLoadFile() throws Exception { // 下載文件,返回字節(jié)數(shù)組 byte[] file_buff = storageClient.download_file("group1", "M00/00/00/wKgBB1l-EwyAGvxuAAWkdYkPHEE854.jpg"); // 將數(shù)據(jù)寫(xiě)文件中 File file = new File("src/test/resources/pic/Ace2.jpg"); FileOutputStream outStream = new FileOutputStream(file); outStream.write(file_buff); outStream.flush(); outStream.close(); }
下面是執(zhí)行結(jié)果,不出意外是可以成功執(zhí)行的
4.獲取元數(shù)據(jù)信息
/** * 元數(shù)據(jù)信息 * @throws Exception * @author: wrh45 * @date: 2017年8月5日下午8:09:38 */ @Test public void metaData() throws Exception { // 上傳圖片的時(shí)候,元數(shù)據(jù)若為空將無(wú)法生存-m的原數(shù)據(jù)文件。獲取時(shí)候此處將拋出NullPointerException NameValuePair[] get_metadata = storageClient.get_metadata("group1", "M00/00/00/wKgBB1l-EwyAGvxuAAWkdYkPHEE854.jpg"); for (NameValuePair nameValuePair: get_metadata) { System.out.println("name: " + nameValuePair.getName() + " value: " + nameValuePair.getValue()); } }
以下是執(zhí)行結(jié)果,獲取到的數(shù)據(jù)和上傳的數(shù)據(jù)一樣
name: fileName value: 測(cè)試專(zhuān)用
name: length value: 測(cè)試專(zhuān)用
5.獲取文件信息
/** * 獲取文件信息 * @author: wrh45 * @date: 2017年8月7日下午9:02:47 */ @Test public void getFileInfo() throws Exception { FileInfo fileInfo = storageClient.get_file_info("group1", "M00/00/00/wKgBB1l-EwyAGvxuAAWkdYkPHEE854.jpg"); System.out.println("CRC32簽名:" + fileInfo.getCrc32()); System.out.println("文件大?。? + fileInfo.getFileSize()); System.out.println("服務(wù)器地址:" + fileInfo.getSourceIpAddr()); System.out.println("創(chuàng)建時(shí)間:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fileInfo.getCreateTimestamp())); }
輸出結(jié)果
CRC32簽名:-1995498431
文件大?。?69781
服務(wù)器地址:192.168.1.7
創(chuàng)建時(shí)間:2017-08-07 20-24-56
6.刪除文件
當(dāng)數(shù)據(jù)測(cè)試完整之后,試試能否刪除吧
/** * 刪除文件 * @throws Exception * @author: wrh45 * @date: 2017年8月7日下午9:10:04 */ @Test public void deleteFile() throws Exception { // 返回0成功,否則返回錯(cuò)誤嗎 int code = storageClient.delete_file("group1", "M00/00/00/wKgBB1l-EwyAGvxuAAWkdYkPHEE854.jpg"); System.out.println(code); }
刪除之后輸出結(jié)果為0,如果出現(xiàn)非0結(jié)果,那么返回的是錯(cuò)誤代碼
以上是Java實(shí)現(xiàn)上傳下載等功能的代碼,如果需要可以封裝成工具類(lèi)使用。
以下是個(gè)人封裝好的工具類(lèi)
/** * 上傳文件工具類(lèi) * @ClassName: FileUploadUtils * @author wrh45 * @date 2017年8月8日下午4:14:31 */ public class FileUploadUtils { private static TrackerClient trackerClient = null; private static TrackerServer trackerServer = null; private static StorageServer storageServer = null; private static StorageClient storageClient = null; private static final String groupName = "group1"; static { // 加載配置文件 try { ClientGlobal.initByProperties("config/fastdfs-client.properties"); // System.out.println("ClientGlobal.configInfo():" + // ClientGlobal.configInfo()); } catch (IOException | MyException e) { e.printStackTrace(); System.out.println("load config file fail"); } } /* * 初始化連接數(shù)據(jù) */ private static void init() { try { trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageClient = new StorageClient(trackerServer, storageServer); } catch (IOException e) { e.printStackTrace(); System.out.println("init fail"); } } /** * 上傳文件 * @param filePath 文件路徑 * @param fileName 文件名稱(chēng) * @return 文件存儲(chǔ)信息 * @author: wrh45 * @date: 2017年8月5日下午11:10:38 */ public static String[] uploadFile(String filePath, String fileName) { return uploadFile(null, filePath, fileName); } /** * 上傳文件 * @param fileBuff 文件字節(jié)數(shù)組 * @param fileName 文件名稱(chēng) * @return 文件存儲(chǔ)信息 * @author: wrh45 * @date: 2017年8月5日下午11:10:38 */ public static String[] uploadFile(byte[] fileBuff, String fileName) { return uploadFile(fileBuff, null, fileName); } /** * 上傳文件 * @param file_buff 文件字節(jié)數(shù)組 * @param filePath 文件路徑 * @param fileName 文件名稱(chēng) * @return 文件存儲(chǔ)信息 * @author: wrh45 * @date: 2017年8月5日下午10:58:19 */ private static String[] uploadFile(byte[] fileBuff, String filePath, String fileName) { try { if (fileBuff == null && filePath == null) { return new String[0]; } // 初始化數(shù)據(jù) if (storageClient == null) { init(); } // 獲取文件擴(kuò)展名稱(chēng) String fileExtName = ""; if (fileName != null && !"".equals(fileName) && fileName.contains(".")) { fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1); } else { return new String[0]; } // 設(shè)置圖片元數(shù)據(jù) NameValuePair[] metaList = new NameValuePair[3]; metaList[0] = new NameValuePair("fileName", fileName); metaList[1] = new NameValuePair("fileExtName", fileExtName); metaList[2] = new NameValuePair("fileSize", String.valueOf(fileBuff.length)); // 上傳文件 String[] uploadFile = null; if (fileBuff != null && filePath == null) { if (fileBuff.length == 0) { return new String[0]; } uploadFile = storageClient.upload_file(fileBuff, fileExtName, metaList); } else { //路徑匹配Windown和Linux if ("".equals(filePath) || !(filePath.matches("^[a-zA-Z]:{1}([\u4e00-\u9fa5\\w/_\\\\-]+)$") || filePath.matches("^(/[\u4e00-\u9fa5\\w_-]+)+$"))) { return new String[0]; } uploadFile = storageClient.upload_file(filePath, fileExtName, metaList); } return uploadFile == null ? new String[0] : uploadFile; } catch (Exception e) { e.printStackTrace(); } finally { try { if (trackerServer != null) { trackerServer.close(); trackerServer = null; } if (storageServer != null) { storageServer.close(); storageServer = null; } } catch (IOException e) { e.printStackTrace(); } } return new String[0]; } /** * 刪除服務(wù)器文件 * @param remoteFileName 文件在服務(wù)器中名稱(chēng) * @author: wrh45 * @date: 2017年8月6日上午12:15:22 */ public static int deleteFile(String remoteFileName) { try { if (remoteFileName == null || "".equals(remoteFileName) || !remoteFileName.contains(groupName)) { return -1; } if (storageClient == null) { init(); } String fileURL = remoteFileName.substring(remoteFileName.indexOf(groupName)); String group = fileURL.substring(0, remoteFileName.indexOf("/") + 1); String fileName = fileURL.substring(remoteFileName.indexOf("/") + 2); int code = storageClient.delete_file(group, fileName); return code; } catch (Exception e) { e.printStackTrace(); System.out.println("The File Delete Fail"); } return -1; } /** * 獲取文件信息 * @param groupName 組名 * @param remoteFilename 遠(yuǎn)程文件名 * @return * @author: wrh45 * @date: 2017年8月8日上午12:25:26 */ public static FileInfo getFileInfo(String groupName, String remoteFilename) { try { if (storageClient == null) { init(); } FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFilename); return fileInfo; } catch (Exception e) { e.printStackTrace(); System.out.println("Get File Info Fail"); } return null; } }
總結(jié)
以上所述是小編給大家介紹的FasfDFS整合Java實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之樹(shù)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之樹(shù),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java數(shù)據(jù)結(jié)構(gòu)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作
這篇文章主要介紹了SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06詳解Java中的反射機(jī)制和動(dòng)態(tài)代理
本文將詳細(xì)介紹反射機(jī)制以及動(dòng)態(tài)代理機(jī)制,而且基本現(xiàn)在的主流框架都應(yīng)用了反射機(jī)制,如spring、MyBatis、Hibernate等等,這就有非常重要的學(xué)習(xí)意義2021-06-06SpringCloud手寫(xiě)Ribbon實(shí)現(xiàn)負(fù)載均衡
這篇文章主要介紹了SpringCloud手寫(xiě)Ribbon實(shí)現(xiàn)負(fù)載均衡的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Spring Boot中使用RabbitMQ 生產(chǎn)消息和消費(fèi)消息的實(shí)例代碼
本文介紹了在SpringBoot中如何使用RabbitMQ進(jìn)行消息的生產(chǎn)和消費(fèi),詳細(xì)闡述了RabbitMQ中交換機(jī)的作用和類(lèi)型,包括直連交換機(jī)、主題交換機(jī)、扇出交換機(jī)和頭交換機(jī),并解釋了各自的消息路由機(jī)制,感興趣的朋友一起看看吧2024-10-10