1. 创建一个名为FastDFS的Java工程, 并导入Jar包
1.1. fastdfs-client-java-1.29-SNAPSHOT.jar是FastDFS的Java API包
1.2. commons-io-2.4.jar是Apache的一个处理IO的工具类包
2. fdfs_client.conf配置
connect_timeout = 2 #网络建立连接超时时间
network_timeout = 30 #网络发送和接收数据超时时间
charset = UTF-8 #编码方式
http.tracker_http_port = 8888 #http的端口号
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
# tracker服务器
tracker_server = 192.168.25.135:22122
tracker_server = 192.168.25.137:22122
tracker_server = 192.168.25.138:22122
3. 上传文件
package com.lywgames.fastdfs;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
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.csource.fastdfs.UploadCallback;
/**
* public String[] upload_file(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String local_filename, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, String master_filename, String prefix_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, String master_filename, String prefix_name, String local_filename, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, String master_filename, String prefix_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list)
* public String[] upload_file(String group_name, String master_filename, String prefix_name, byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list)
*
*/
public class UploadFile {
public static final String uploadPathPre = "./upload/";
public static String originalFileName = "";
public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
// 加载配置文件
ClientGlobal.init("./fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
upload_file10(storageClient);
}
public static void recordFileName(String[] upload_file) throws IOException {
StringBuffer sb = new StringBuffer();
for (String string : upload_file) {
System.out.println(string);
sb.append(string).append("#");
}
sb.append(originalFileName).append("\r\n");
FileUtils.write(new File("./download/uploadfilename.txt"), sb.toString(), "utf-8", true);
}
/**
* 通过字节数组上传文件到储存服务器, 适合上传小文件
*/
public static void upload_file1(StorageClient storageClient) throws Exception {
originalFileName = "01.png";
byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));
String[] upload_file = storageClient.upload_file(buffer, "png", null);
recordFileName(upload_file);
}
/**
* 通过文件名上传文件到储存服务器, 适合上传小文件
*/
public static void upload_file2(StorageClient storageClient) throws Exception {
originalFileName = "02.png";
String[] upload_file = storageClient.upload_file(uploadPathPre + originalFileName, "png", null);
recordFileName(upload_file);
}
/**
* 通过字节数组上传文件到指定卷/组的储存服务器, 适合上传小文件
*/
public static void upload_file3(StorageClient storageClient) throws Exception {
originalFileName = "03.png";
byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));
String[] upload_file = storageClient.upload_file("group2", buffer, "png", null);
recordFileName(upload_file);
}
/**
* 通过字节数组上传文件到储存服务器(可以设置偏移量和长度), 适合上传小文件
*/
public static void upload_file4(StorageClient storageClient) throws Exception {
originalFileName = "04.png";
byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));
String[] upload_file = storageClient.upload_file(buffer, 0, buffer.length, "png", null);
recordFileName(upload_file);
}
/**
* 设置上传文件回调上传文件到储存服务器, 适合上传大文件
*/
public static void upload_file5(StorageClient storageClient) throws Exception {
originalFileName = "001.mp4";
File file = new File(uploadPathPre + originalFileName);
String[] upload_file = storageClient.upload_file("group2", file.length(), new UploadCallback() {
@Override
public int send(OutputStream out) throws IOException {
int readBytes = -1;
byte[] buff = new byte[256 * 1024];
FileInputStream fis = new FileInputStream(uploadPathPre + originalFileName);
try {
while ((readBytes = fis.read(buff)) != -1) {
out.write(buff, 0, readBytes);
}
out.flush();
} finally {
fis.close();
}
return 0;
}
}, "mp4", null);
recordFileName(upload_file);
}
/**
* 通过字节数组上传文件到指定卷/组的储存服务器(可以设置偏移量和长度), 适合上传小文件
*/
public static void upload_file6(StorageClient storageClient) throws Exception {
originalFileName = "05.png";
byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));
String[] upload_file = storageClient.upload_file("group2", buffer, 0, buffer.length, "png", null);
recordFileName(upload_file);
}
/**
* 通过字节数组上传关联文件到指定卷/组的储存服务器, 适合上传小文件
* 如: thum180_01.png是01.png的缩略图
*/
public static void upload_file7(StorageClient storageClient) throws Exception {
originalFileName = "thum180_01.png";
byte[] buffer = FileUtils.readFileToByteArray(new File(uploadPathPre + originalFileName));
String[] upload_file = storageClient.upload_file("group1", "M00/00/00/wKgZh17ndBKAdW0AAARREaA3Y7E015.png", "-thum180", buffer, "png", null);
recordFileName(upload_file);
}
/**
* 通过文件名上传关联文件到指定卷/组的储存服务器, 适合上传小文件
*/
public static void upload_file8(St