FastDFS distributed file system uses

Installation reference:
http://blog.csdn.net/hardworking0323/article/details/51055304

http://blog.csdn.net/zhu_tianwei/article/details/46045641

Official forum: http://bbs.chinaunix.net/ forum-240-1.htmlStartup

sequence:

start fdfs_storaged:

/etc/init.d/fdfs_storaged start
start:
trackerd start:
/etc/init.d/fdfs_trackerd start

nginx start:
/usr/fastdfs/nginx-1.12.1/ objs
[root@yunboce objs]# ./nginx

There is an installer in the attachment.


Next, use the Java program to access the fastdfs service
package fastdfs.sample;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ClientTest {

/**
* 上传文件
* @return
*/
public String updateFile(){
File file = new File("F:\\down\\gao.jpg");
FastDfsClient fastDfsClient= FastDfsClient.getInstance();

String result = fastDfsClient.uploadFile(file, file.getName());
System.out.println(result);

return result;
}

/**
* 上传文件时,添加属性
* @return
*/
public String updateFileMeta(){
File file = new File("F:\\down\\gao.jpg");
Map<String,String> metaList = new HashMap<String,String>();
metaList.put("width", "100px");
metaList.put("height", "200px");
metaList.put("author","boce");

FastDfsClient fastDfsClient= FastDfsClient.getInstance();
String result = fastDfsClient.uploadFile(file, file.getName(),metaList);
System.out.println("meta:"+result);

return result;
}

/**
* View File properties
* @param fileId
*/
public void fileMeta(final String fileId){

FastDfsClient fastDfsClient= FastDfsClient.getInstance();

Map<String,String> result = fastDfsClient.getFileMetadata(fileId);
System.out.println("meta: "+result.toString());

}


/**
* Delete file
*/
public void deletefile(){
//Upload is the path returned
String fileId="group1/M00/00/00/wKgGGFmmDpaAY0nGAEc2_Sy0FSM264.png";
FastDfsClient fastDfsClient= FastDfsClient.getInstance();

int del = fastDfsClient.deleteFile(fileId);
    System.out.println("result="+del);
}


public void downfile(String fileId){
//上传是返回的路径
//String fileId ="group1/M00/00/00/wKgGGFmlMCSAAI6_AAMhasozgRc.1.jpg1";
    File path = new File("f:\\down\\");
    if(!path.exists()){
    path.mkdirs();
    }
   
    File outFile = new File(path, "gao1.jpg");
    if(!outFile.exists()){
    try {
outFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
   
FastDfsClient fastDfsClient= FastDfsClient.getInstance();
    int dInt = fastDfsClient.downloadFile(fileId, outFile);
    System.out.println(dInt);


}

public static void main(String[] args) {
ClientTest t = new ClientTest();
// String fileId = t.updateFile();
// //group1/M00/00/00/wKgGGFmmIf2AasNTAAMhasozgRc105.jpg
// t.downfile(fileId);


String fileMeta = t.updateFileMeta();
t.fileMeta(fileMeta);


}

}




测试代码:
package fastdfs.sample;

import java.io.File;
import java.io.IOException;

public class ClientTest {

public String updateFile(){
File file = new File("F:\\down\\gao.jpg");
FastDfsClient fastDfsClient= FastDfsClient.getInstance();

String result = fastDfsClient.uploadFile(file, file.getName());
System.out.println(result);

return result;
}


public void deletefile(){
//上传是返回的路径
String fileId="group1/M00/00/00/wKgGGFmmDpaAY0nGAEc2_Sy0FSM264.png";
FastDfsClient fastDfsClient= FastDfsClient.getInstance();

int del = fastDfsClient.deleteFile(fileId);
    System.out.println("result="+del);
}


public void downfile(String fileId){
//上传是返回的路径
//String fileId ="group1/M00/00/00/wKgGGFmlMCSAAI6_AAMhasozgRc.1.jpg1";
    File path = new File("f:\\down\\");
    if(!path.exists()){
    path.mkdirs();
    }
   
    File outFile = new File(path, "gao1.jpg");
    if(!outFile.exists()){
    try {
outFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
   
FastDfsClient fastDfsClient= FastDfsClient.getInstance();
    int dInt = fastDfsClient.downloadFile(fileId, outFile);
    System.out.println(dInt);


}

public static void main(String[] args) {
ClientTest t = new ClientTest();
String fileId = t.updateFile();
//group1/M00/00/00/wKgGGFmmIf2AasNTAAMhasozgRc105.jpg

t.downfile(fileId);


}

}







The source code of the project is placed in ssm_dream.rar 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262498&siteId=291194637