一、 下载 https://github.com/happyfish100/fastdfs-client-java.git
二、解压缩 安装到本地仓库
<!-- fastdfs java依赖 -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
三、准备配置文件 fdfs_client.conf 放置在src下
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.128.141:22122
注1:tracker_server指向您自己IP地址和端口,1-n个
注2:除了tracker_server,其它配置项都是可选的
四、测试
测试上传
private String conf_filename="fdfs_client.conf"; @Test public void testUpload(){ try { ClientGlobal.init(conf_filename); TrackerClient client = new TrackerClient(); TrackerServer server = client.getConnection(); StorageClient storageClient = new StorageClient(server, null); NameValuePair[] nameValuePairs = new NameValuePair[]{ new NameValuePair("id","100"), new NameValuePair("name","小红") }; String[] file = storageClient.upload_file("d:\\c.jpg", "jpg", nameValuePairs); for (String s : file) { System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } }
测试下载
@Test public void testDownLoad(){ try { ClientGlobal.init(conf_filename); TrackerServer trackerServer = new TrackerClient().getConnection(); StorageClient storageClient = new StorageClient(trackerServer, null); byte[] bytes = storageClient.download_file("group1", "M00/00/00/wKiAjVlpGguASKnyAADQcgmZjMs583.jpg"); FileOutputStream stream = new FileOutputStream("test.jpg"); stream.write(bytes); stream.close(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } }
测试删除
@Test public void testDelete(){ try { ClientGlobal.init(conf_filename); TrackerServer trackerServer = new TrackerClient().getConnection(); StorageClient storageClient = new StorageClient(trackerServer, null); int i= storageClient.delete_file("group1", "M00/00/00/wKiAjVlpGguASKnyAADQcgmZjMs583.jpg"); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } }
测试获取元数据
@Test public void testGetMetaData(){ try { ClientGlobal.init(conf_filename); TrackerServer trackerServer = new TrackerClient().getConnection(); StorageClient storageClient = new StorageClient(trackerServer, null); NameValuePair[] metadata = storageClient.get_metadata("group1", "M00/00/00/wKiAjVlpHBuAJE_yAADQcgmZjMs320.jpg"); for (NameValuePair metadatum : metadata) { System.out.println(metadatum.getName()+" "+metadatum.getValue()); } } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } }
测试获取文件信息
@Test public void testGetFileInfo(){ try { ClientGlobal.init(conf_filename); TrackerServer trackerServer = new TrackerClient().getConnection(); StorageClient storageClient = new StorageClient(trackerServer, null); FileInfo fileInfo = storageClient.get_file_info("group1", "M00/00/00/wKiAiVlkVcqALRgEAAAABudnRxc478.txt"); System.out.println(fileInfo.getSourceIpAddr()+" "+fileInfo.getCreateTimestamp()+" "+fileInfo.getFileSize()+" "+fileInfo.getCrc32()); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } }