要想搭建fastDFS網上有相近的文章:html
分佈式文件系統 - FastDFS 在 CentOS 下配置安裝部署java
分佈式文件系統 - FastDFS 配置 Nginx 模塊及上傳測試linux
首先下載fastdfs-client-java jar包,我是下載fastdfs-client-java源碼,本身打的jar包,fastdfs-client-java源碼nginx
首先建立java項目git
建立fdfs_client.conf文件github
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.206.139:22122 #對應本身的tracker服務IP地址
這裏只測試了上傳一張圖片,而後下載這張圖片apache
具體java代碼:瀏覽器
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.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer; import org.junit.Test; public class FastDFS { @Test public void upload(){ try {// 初始化全局配置。加載一個配置文件。 ClientGlobal.init("D:\\workspace-12306\\Test\\src\\test\\resources\\fdfs_client.conf"); // 建立一個TrackerClient對象。 TrackerClient trackerClient = new TrackerClient(); // 建立一個TrackerServer對象。 TrackerServer trackerServer = trackerClient.getConnection(); // 聲明一個StorageServer對象,null。 StorageServer storageServer = null; // 得到StorageClient對象。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 直接調用StorageClient對象方法上傳文件便可。 String[] strings; strings = storageClient.upload_file("D:\\1.jpg", "jpg", null); for (String string : strings) { System.out.println(string); } } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } } @Test public void download(){ try { // 初始化全局配置。加載一個配置文件 ClientGlobal.init("D:\\workspace-12306\\Test\\src\\test\\resources\\fdfs_client.conf"); // 建立一個TrackerClient對象 TrackerClient tracker = new TrackerClient(); // 建立一個TrackerServer對象。 TrackerServer trackerServer = tracker.getConnection(); // 聲明一個StorageServer對象, StorageServer storageServer = null; // 得到StorageClient對象 StorageClient storageClient = new StorageClient(trackerServer, storageServer); byte[] b = storageClient.download_file("group1", "M00/00/00/wKjOi1gi0nOAcSo8AAA63pKOZZ0312.jpg"); System.out.println(b); // 將下載的文件流保存 IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".jpg")); } catch (Exception e) { e.printStackTrace(); } } }
在開始測試時遇到一個異常:安全
connect to server 192.168.206.139:22122 fail java.net.SocketTimeoutException: connect timed out at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:47) at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:72) at org.csource.fastdfs.TrackerClient.getConnection(TrackerClient.java:58) at FastDFS.upload(FastDFS.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) connect to server 192.168.206.139:22122 fail java.net.SocketTimeoutException: connect timed out at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:47) at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:72) at org.csource.fastdfs.TrackerClient.getConnection(TrackerClient.java:58) at org.csource.fastdfs.TrackerClient.getStoreStorage(TrackerClient.java:91) at org.csource.fastdfs.StorageClient.newWritableStorageConnection(StorageClient.java:1938) at org.csource.fastdfs.StorageClient.do_upload_file(StorageClient.java:703) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:164) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:132) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:114) at FastDFS.upload(FastDFS.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) org.csource.common.MyException: getStoreStorage fail, errno code: 0 at org.csource.fastdfs.StorageClient.newWritableStorageConnection(StorageClient.java:1941) at org.csource.fastdfs.StorageClient.do_upload_file(StorageClient.java:703) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:164) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:132) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:114) at FastDFS.upload(FastDFS.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
鏈接超時問題,這是因爲linux沒有開放22122端口和23000端口所致,咱們能夠關閉linux的防火牆iptables,可是不推薦這樣作,會有安全問題,建議將這兩個端口添加到防火牆開放端口中:app
#關閉防火牆命令
service iptables stop
#將22111和23000端口添加到防火牆中 /sbin/iptables -I INPUT -p tcp --dport 22122 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 23000 -j ACCEPT #保存iptables /etc/rc.d/init.d/iptables save #重啓iptables service iptables restart
配置完iptables,再測試上傳就會成功,打印出上傳信息:
能夠將打印的上傳信息在瀏覽器中查看是否上傳成功:
在瀏覽器中可以正常顯示,說明長傳成功。
在D盤可以找到保存的圖片,說明下載成功。