您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何在Java項(xiàng)目中利用FasfDFS實(shí)現(xiàn)一個(gè)文件上傳下載功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
一 : 添加配置文件
當(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è)試,為了方便在開始執(zhí)行之前,初始化配置文件和獲取連接,同時(shí)沒有捕獲異常全部拋出
1.初始化連接信息
//成員變量 TrackerServer trackerServer = null; StorageServer storageServer = null; StorageClient storageClient = null; /** * 初始化連接信息 * @author: wrh55 * @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ù)端口是否開啟。默認(rèn)為22122和23000
2.上傳文件
/** * 上傳圖片 * @throws Exception * @author: wrh55 * @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è)試專用"); meta_list[1] = new NameValuePair("length", "測(cè)試專用"); // 文件上傳,返回組名和訪問(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è)地址試試能否訪問(wèn)
OK,在瀏覽器輸入地址后,成功返回了圖片信息
3.下載文件
這里通過(guò)storageClient下載文件,然后將數(shù)據(jù)存儲(chǔ)到本地。如果通過(guò)瀏覽器下載,同理將數(shù)據(jù)寫入文件即可
/** * 下載圖片 * @throws Exception * @author: wrh55 * @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ù)寫文件中 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: wrh55 * @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è)試專用
name: length value: 測(cè)試專用
5.獲取文件信息
/** * 獲取文件信息 * @author: wrh55 * @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("文件大?。?quot; + 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
文件大小:369781
服務(wù)器地址:192.168.1.7
創(chuàng)建時(shí)間:2017-08-07 20-24-56
6.刪除文件
當(dāng)數(shù)據(jù)測(cè)試完整之后,試試能否刪除吧
/** * 刪除文件 * @throws Exception * @author: wrh55 * @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)上傳下載等功能的代碼,如果需要可以封裝成工具類使用。
以下是個(gè)人封裝好的工具類
/** * 上傳文件工具類 * @ClassName: FileUploadUtils * @author wrh55 * @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 文件名稱 * @return 文件存儲(chǔ)信息 * @author: wrh55 * @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 文件名稱 * @return 文件存儲(chǔ)信息 * @author: wrh55 * @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 文件名稱 * @return 文件存儲(chǔ)信息 * @author: wrh55 * @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ò)展名稱 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ù)器中名稱 * @author: wrh55 * @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: wrh55 * @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; } }
以上就是如何在Java項(xiàng)目中利用FasfDFS實(shí)現(xiàn)一個(gè)文件上傳下載功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。