20.fastDFS Cluster Java Code test

Source: Internet
Author: User

1. Engineering structure 2. Code contentFastdfsclienttest.java Code
Package Cn.itcast.fastdfs.cliennt;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.util.uuid;import Org.csource.common.NameValuePair ; Import Org.csource.fastdfs.clientglobal;import Org.csource.fastdfs.fileinfo;import Org.csource.fastdfs.storageclient;import Org.csource.fastdfs.storageclient1;import Org.csource.fastdfs.storageserver;import Org.csource.fastdfs.trackerclient;import Org.csource.fastdfs.trackerserver;import org.junit.before;import org.junit.test;/** * test Fastdfs &NBSP;API use  * @FileName: fastdfsclienttest.java * @Description: TODO ()   * @Author: tangwan * @Date: April 23, 2015 PM 1:48:45 * @since:  jdk 1.8 */public class Fastdfsclienttest { public String conf_ filename = null;public String local_filename = null;  @Beforepublic void Confinit () {conf_filename = FastdfsClientTest.class.getResource ("/fdfs_client.conf"). GetPath (); SYSTEM.OUT.PRINTLN ("Configure Folder path:"+conf_filename"); local_filename = "1.jpg"; System.out.println ("File path to upload:" +local_filename);}  //Client configuration file (absolute path), using the path annotated below, note the Confinit method above//public String conf_filename = "d:\\debuglove\\workspace\\dev-2\\ Fastdfsclient\\src\\cn\\itcast\\fastdfs\\cliennt\\fdfs_client.conf "///local file, file to be uploaded//public String local_filename = "C:\\users\\administrator\\desktop\\2.jpg";  //writes a byte stream to a disk-generated file private void SaveFile (byte[] B, string path, string FileName) { file file = new file (path + filename); FileOutputStream FileOutputStream = null;try {fileoutputstream = new FileOutputStream (file);  Fileoutputstream.write (b);  } catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) { E.printstacktrace ();} finally {if (FileOutputStream! = null) {try {fileoutputstream.close ()} catch (IOException e) {e.printstacktrace ()}}  } } //upload File//file saved under Storage settings BaseURL  /home/fastdfs/fdfs_storage/data/00/00 include: Wkgbfvcmjnyaztdnaasucjwyjng393.jpg and Wkgbfvcmjnyaztdnaasucjwyjng393.jpg-m@testpublic void Testupload () {//Upload 5 times for (int i = 0; i < 1; i++) {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[] {new Namevaluepair ("item_id", "100010"), New Namevaluepair ("width", "+"), new N Amevaluepair ("Height", "90")}; String fileids[] = storageclient.upload_file (Local_filename, NULL, NVP);  system.out.println (fileids.length); SYSTEM.OUT.PRINTLN ("group name:" + fileids[0]); System.out.println ("Path:" + fileids[1]);  } catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ()}}}  //download file @testpublic void Testdownload () {try {clientglobal.init (conf_filename);  trackerclient tracker = new Trackerclient (); Trackerserver Trackerserver =Tracker.getconnection (); Storageserver storageserver = null; //storageclient storageclient = new Storageclient (TrackerServer, Storageserver); StorageClient1 storageclient = new StorageClient1 (Trackerserver, storageserver); byte[] B = storageclient.download_file ("Group1", "m00/00/00/wkgbfvcmjn6aguyuaasucjwyjng476.jpg"), if (b! = null) {System.out.println (b.length); SaveFile (b, "D:\\temp\\", Uuid.randomuuid (). toString () + ". jpg");  } catch (Exception e) {e.printstacktrace ()}}  //get file information @testpublic 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/wkgbfvcmjn6aguyuaasucjwyjng476.jpg"); System.out.println (Fi.getsourceipaddr ()); System.out.println (Fi.getfilesize ()); System.out.println (fi.getcReatetimestamp ()); System.out.println (FI.GETCRC32 ());} catch (Exception e) {e.printstacktrace ();}}  //gets the file custom mate information (key, value) @Testpublic 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/wkgbfvcmjn6aguyuaasucjwyjng476.jpg"); Nvps! = null) {for (Namevaluepair Nvp:nvps) {System.out.println (Nvp.getname () + ":" + nvp.getvalue ());}}  } catch (Exception e) {e.printstacktrace ()}}  //Delete file//from the operating system storage node, physical deletion/home/fastdfs/fdfs_storage/data/00/00 directly disappears @testpublic void Testdelete () {try { Clientglobal.init (conf_filename);  trackerclient tracker = new Trackerclient (); Trackerserver trackerserver = Tracker.getconnection (); Storageserver Storageserver = null; storageclientStorageclient = new Storageclient (Trackerserver, storageserver); int i = Storageclient.delete_file ("group1", "M00/00/00 /wkgbfvcmjnyaztdnaasucjwyjng393.jpg "); System.out.println (i = = 0?) "Delete succeeded": "Delete failed:" + i);} catch (Exception e) {e.printstacktrace ();}}}
2.fdfs_client.conf #配置文件 
# Connect Timeout in seconds# default value was 30sconnect_timeout=30# network timeout in seconds# default value is 30SNETW Ork_timeout=60  # The base path to store log files Base_path=/home/fastdfs# tracker_server can ocur more than once, and Tracker_server format is# "Host:port", host can hostname or IP address tracker_server=192.168.1.20:22122#tracker_server =192.168.101.4:22122#standard Log level as syslog, case insensitive, value list:### Emerg for emergency### alert### crit for critical### error### warn for warning### notice### info### debuglog_level=info  # If Use connection pool# default value is false# since V4.05use_connection_pool = False  # Connections whose the idle time exceeds this time would be being closed# unit:second# default value is 3600# since V4.05connec Tion_pool_max_idle_time = 3600# If load Fastdfs parameters from tracker server# since v4.05# default value is FALSELOAD_FD fs_parameters_from_tracker=false# if use storage ID instead of IP address# same as tracker.conf# valid if Load_fdfs _parameters_from_tracker is false# default value is false# since v4.05use_storage_id = false# Specify storage IDs filename , can use relative or absolute path# same as tracker.conf# valid if Load_fdfs_parameters_from_tracker is false# sin Ce v4.05storage_ids_filename = storage_ids.conf    #HTTP settings http.tracker_server_port=80#use "#include" directive to include HTTP other settiongs# #include http.conf
3. Files to be uploaded, beauty, show us 4.jar package download https://sourceforge.net/projects/fastdfs/



From for notes (Wiz)



20.fastDFS Cluster Java Code test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.