具体介绍与安装看这个,一步步来
CentOS7 X64
用FastDFS一步步搭建文件管理系统
java api的使用。
maven项目
pom.xml
<dependencies>
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
</dependencies>
fdfs_client.conf resources目录下的配置文件,只需要修改ip
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.163.132:22122
Upload.java 上传使用
package fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.*;
public class Upload {
public static void main(String[] args) {
try{
ClientGlobal.init("fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair[] nameValuePairs = new NameValuePair[2];
nameValuePairs[0] = new NameValuePair("file","filename");
nameValuePairs[1] = new NameValuePair("file2","file2name");
byte[] files = Upload.getBytes("fdfs_client.conf");
String[] uploadResults = storageClient.upload_file(files,"txt",nameValuePairs);
//数组0,group
//数组1,文件路径
for(String one:uploadResults)
{
System.out.println(one);
}
}catch (Exception e){
e.printStackTrace();
}
}
public static byte[] getBytes(String filePath){
byte[] buffer = null;
try {
File file = new File(ClassLoader.getSystemResource(filePath).toURI());
System.out.println(file.getAbsolutePath());
;
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
return buffer;
}
}
Download.java 下载使用
package fastdfs;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Download {
public static void main(String[] args) {
String group="group1";
String path="M00/00/00/wKijhFs8cdOAXHteAAAAvuh0edM875.txt";
try{
ClientGlobal.init("fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] bytes = storageClient.download_file(group,path);
System.out.println(bytes.length);
Download.toFile(bytes,".","test.txt");
}catch (Exception e){
e.printStackTrace();
}
}
public static void toFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+"//"+fileName);
System.out.println(file.getAbsolutePath());
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}