Java高级技术FastDFS的学习
生活随笔
收集整理的這篇文章主要介紹了
Java高级技术FastDFS的学习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.定義 FastDFS 工具類
package an.cxy.util;import org.csource.fastdfs.*;public class ClientGlobalUtil {private static StorageClient storageClient;private static final String path = "fdfs.conf";static {try{ClientGlobal.init(path);TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);storageClient = new StorageClient(trackerServer,storeStorage);}catch (Exception e){e.printStackTrace();}}public static StorageClient getStorageClient(){return storageClient;}/* public static void main(String[] args) {System.out.println(getStorageClient());}*/ }2.上傳文件測(cè)試
packagesinge; import org.csource.common.NameValuePair; import org.csource.fastdfs.StorageClient; import org.junit.Test; public class TestFileUpload{ @Test public void test() { StringfileIds[] =testUpload("testFile", "1.jpg","jpg"); if(fileIds==null){ System.out.println("文件上傳失敗!"); } System.out.println(fileIds.length); System.out.println("組名/卷名:"+ fileIds[0]); System.out.println("路徑:"+fileIds[1]); } public String[] testUpload(StringdirName,StringfileName,StringfileType){ StorageClientstorageClient=FastDFSClientUtils.getStorageClient(); try{ // 要上傳的文件的元數(shù)據(jù)。可以自定義。 NameValuePairnvp[]=newNameValuePair[]{ newNameValuePair("filename",fileName) }; /* * String[] upload_file(String fileName, String fileExtName, NameValuePair[] metaList) *fileName - 要上傳的本地文件,包含路徑地址。 * fileExtName - 文件后綴名;可以不傳遞,也就是傳遞一個(gè) null。FastDFS 可以自動(dòng)的在文件名中截取文件類型。 *metaList - 文件的描述元數(shù)據(jù)。 * 返回的結(jié)果是一個(gè)字符串?dāng)?shù)組。長(zhǎng)度為 2。0 位置是卷名-group1,1 位置是 文件名-M00/00/00/xxxxxx */ String fileIds[] =storageClient.upload_file(dirName+"/"+fileName,fileType,nvp);return fileIds; }catch(Exceptione){ e.printStackTrace(); } returnnull;} }總結(jié)
以上是生活随笔為你收集整理的Java高级技术FastDFS的学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NLP - sentencepiece
- 下一篇: [羊城杯 2020]A Piece Of