本次博客带大家来通过java来实现对分布式存储系统的上传下载增删改查等常见操作。
(1)下载fastdfs-client-java文件:fastdfs-client-java
(2)导入eclipse并且export成fastdfs_client.jar
(3)cmd下安装fastdfs_client.jar到mvn环境
pom.xml如下,主要是依赖包那一块,其他的是系统生成的
win+ R
cmd
mvn install:install-file -DgroupId = org. csource -DartifactId = fastdfs-client -java -Dversion = 5.0 .4 -Dpackaging = jar -Dfile = C:\ Users\ administr\ Desktop\ fastdfs_client. jar
(4)新建一个项目maven的fastdfs-demo测试这个jar包的使用
<project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion > 4.0.0</modelVersion >
<groupId > org.csource</groupId >
<artifactId > fastdfs-client-java</artifactId >
<version > 1.27-SNAPSHOT</version >
<name > fastdfs-client-java</name >
<description > fastdfs client for java</description >
<packaging > jar</packaging >
<properties >
<project.build.sourceEncoding > UTF-8</project.build.sourceEncoding >
<project.reporting.outputEncoding > UTF-8</project.reporting.outputEncoding >
<maven.test.failure.ignore > true</maven.test.failure.ignore >
<maven.test.skip > true</maven.test.skip >
<jdk.version > 1.6</jdk.version >
</properties >
<build >
<plugins >
<plugin >
<groupId > org.apache.maven.plugins</groupId >
<artifactId > maven-compiler-plugin</artifactId >
<version > 3.5.1</version >
<configuration >
<encoding > UTF-8</encoding >
<source > ${jdk.version}</source >
<target > ${jdk.version}</target >
</configuration >
</plugin >
</plugins >
</build >
</project >
(5)在src/main/resources中新建fdfs_client.conf文件,内容如下
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.50.203:22122
(6)
在src/main/java中创建TestFastDfs.java如下
package com .xj .fastdfs _demo
import java.io .FileNotFoundException
import java.io .FileOutputStream
import java.io .IOException
import java.util .UUID
import org.apache .commons .io .IOUtils
import org.csource .common .MyException
import org.csource .common .NameValuePair
import org.csource .fastdfs .ClientGlobal
import org.csource .fastdfs .FileInfo
import org.csource .fastdfs .StorageClient
import org.csource .fastdfs .StorageServer
import org.csource .fastdfs .TrackerClient
import org.csource .fastdfs .TrackerServer
import org.junit .After
import org.junit .Before
import org.junit .Test
public class TestFastDfs {
//刚刚的配置文件位置
public String conf_filename = "D:\\MyProject\\fastdfs-demo\\src\\main\\resources\\fdfs_client.conf"
//要上传的文件地址
public String local_filename = "C:\\Users\\zxj\\Desktop\\新建文本.txt"
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testUpload() {
try {
ClientGlobal.init (conf_filename)
TrackerClient tracker = new TrackerClient()
TrackerServer trackerServer = tracker.getConnection ()
StorageServer storageServer = null
StorageClient storageClient = new StorageClient(trackerServer, storageServer)
// NameValuePair nvp = new NameValuePair("age" , "18" )
NameValuePair nvp [] = new NameValuePair[]{
new NameValuePair("age" , "18" ),
new NameValuePair("sex" , "male" )
}
String fileIds[] = storageClient.upload _file(local_filename, "png" , nvp)
System.out .println (fileIds.length )
System.out .println ("组名:" + fileIds[0 ])
System.out .println ("路径: " + fileIds[1 ])
} catch (FileNotFoundException e) {
e.printStackTrace ()
} catch (IOException e) {
e.printStackTrace ()
} catch (MyException e) {
e.printStackTrace ()
}
}
public void testDownload() {
try {
ClientGlobal.init (conf_filename)
TrackerClient tracker = new TrackerClient()
TrackerServer trackerServer = tracker.getConnection ()
StorageServer storageServer = null
StorageClient storageClient = new StorageClient(trackerServer, storageServer)
byte[] b = storageClient.download _file("group1" , "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" )
System.out .println (b)
IOUtils.write (b, new FileOutputStream("D:/" +UUID.randomUUID ().toString ()+".conf" ))
} catch (Exception e) {
e.printStackTrace ()
}
}
public void testGetFileInfo(){
try {
ClientGlobal.init (conf_filename)
TrackerClient tracker = new TrackerClient()
TrackerServer trackerServer = tracker.getConnection ()
StorageServer storageServer = null
StorageClient storageClient = new StorageClient(trackerServer, storageServer)
FileInfo fi = storageClient.get _file_info("group1" , "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" )
System.out .println (fi.getSourceIpAddr ())
System.out .println (fi.getFileSize ())
System.out .println (fi.getCreateTimestamp ())
System.out .println (fi.getCrc 32())
} catch (Exception e) {
e.printStackTrace ()
}
}
public void testGetFileMate(){
try {
ClientGlobal.init (conf_filename)
TrackerClient tracker = new TrackerClient()
TrackerServer trackerServer = tracker.getConnection ()
StorageServer storageServer = null
StorageClient storageClient = new StorageClient(trackerServer,
storageServer)
NameValuePair nvps [] = storageClient.get _metadata("group1" , "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" )
for(NameValuePair nvp : nvps){
System.out .println (nvp.getName () + ":" + nvp.getValue ())
}
} catch (Exception e) {
e.printStackTrace ()
}
}
public void testDelete(){
try {
ClientGlobal.init (conf_filename)
TrackerClient tracker = new TrackerClient()
TrackerServer trackerServer = tracker.getConnection ()
StorageServer storageServer = null
StorageClient storageClient = new StorageClient(trackerServer,
storageServer)
int i = storageClient.delete _file("group1" , "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf" )
System.out .println ( i==0 ? "删除成功" : "删除失败:" +i)
} catch (Exception e) {
e.printStackTrace ()
}
}
}
(7)验证工作
点击运行junit框架测试上传操作是否成功
此时已经上传成功,我们到存储节点slaver2查看文件验证
$# cd /home/sfs/fastdfs_storage/data/00 /00
我们发现文件已经上传成功,表明java操作FastDFS成功,后面的下载和删除修改等操作都类似。