FastDFS分布式文件系统使用

安装参照:
http://blog.csdn.net/hardworking0323/article/details/51055304

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

官方论坛:http://bbs.chinaunix.net/forum-240-1.html

启动顺序:

启动fdfs_storaged :

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

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

附件中有安装程序


下面使用Java程序访问 fastdfs服务
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;
}

/**
* 查看文件属性
* @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());

}


/**
* 删除文件
*/
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);


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);


}

}







项目源代码放在 ssm_dream.rar 中 

猜你喜欢

转载自gjp014.iteye.com/blog/2391532