FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片、视频、文档等文件。对于互联网应用,和其他分布式文件系统相比,优势非常明显。
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
import java.io.File;
import java.io.FileInputStream;
import org.apache.log4j.Logger;
import org.csource.common.IniFileReader;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSUtils
{
private static Logger log = Logger.getLogger(FastDFSUtils.class);
public static String getFileName(String filePath)
{
String fileName = null;
if (filePath != null)
{
int index = filePath.replace("\\", "/").lastIndexOf("/");
fileName = filePath.substring(index);
}
return fileName;
}
public static String getFileFormat(String fileName)
{
String fileFormat = null;
if (fileName != null && fileName.contains("."))
{
String[] arry = fileName.split("[.]");
fileFormat = arry[1];
}
return fileFormat;
}
public static FileVO upload(String filePath, String configRootPath,
String oldFileName, String fileFormat)
throws Exception
{
File file = new File(filePath);
FileVO fileVo = upload(file, configRootPath, oldFileName, fileFormat);
return fileVo;
}
public static FileVO upload(File file, String configRootPath,
String oldFileName, String fileFormat)
throws Exception
{
boolean isOK = false;
long uploadTime = 0;
String groupName = null;
String newFileName = null;
long fileSize = 0;
String classPath = configRootPath == null ? new File(FastDFSUtils.class
.getResource("/").getFile()).getCanonicalPath()
: configRootPath;
String configFilePath = classPath + File.separator + "fdfs_client.conf";
if (new File(configFilePath.replace("%20", " ")).exists())
{
System.out.println("配置文件:" + configFilePath);
ClientGlobal.init(configFilePath.replace("%20", " "));
IniFileReader iniReader = new IniFileReader(configFilePath.replace(
"%20", " "));
groupName = iniReader.getStrValue("groupName");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("width", "120");
meta_list[1] = new NameValuePair("heigth", "120");
meta_list[2] = new NameValuePair("author", "gary");
FileInputStream fis = new FileInputStream(file);
byte[] file_buff = null;
if (fis != null)
{
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
log.info("file length: " + file_buff.length);
StorageServer[] storageServers = trackerClient.getStoreStorages(
trackerServer, groupName);
if (storageServers == null)
{
log.error("get store storage servers fail, error code: "
+ storageClient.getErrorCode());
}
else
{
log.info("store storage servers count: "
+ storageServers.length);
for (int k = 0; k < storageServers.length; k++)
{
log.info(k
+ 1
+ ". "
+ storageServers[k].getInetSocketAddress()
.getAddress().getHostAddress()
+ ":"
+ storageServers[k].getInetSocketAddress()
.getPort());
}
}
long startTime = System.currentTimeMillis();
/*
* String[] results = storageClient.upload_file(file_buff,
* fileFormat, meta_list);
*/
String[] results = storageClient.upload_file(groupName, file_buff,
fileFormat, meta_list);
uploadTime = System.currentTimeMillis() - startTime;
log.info("upload_file time used: " + uploadTime + " ms");
fis.close();
if (results == null)
{
log.error("upload file fail, error code: "
+ storageClient.getErrorCode());
throw new Exception("upload file fail, error code: "
+ storageClient.getErrorCode());
}
isOK = true;
// groupName = results[0];
newFileName = results[1];
log.info("group_name: " + groupName + ", remote_filename: "
+ newFileName);
log.info(storageClient.get_file_info(groupName, newFileName));
ServerInfo[] servers = trackerClient.getFetchStorages(
trackerServer, groupName, newFileName);
if (servers == null)
{
log.error("get storage servers fail, error code: "
+ trackerClient.getErrorCode());
}
else
{
log.info("storage servers count: " + servers.length);
for (int k = 0; k < servers.length; k++)
{
log.info(k + 1 + ". " + servers[k].getIpAddr() + ":"
+ servers[k].getPort());
}
}
}
else
{
log.error("配置文件不存在!" + configFilePath);
throw new Exception("配置文件不存在!");
}
FileVO fileVo = new FileVO(isOK, groupName, oldFileName, newFileName,
fileSize, uploadTime, fileFormat);
return fileVo;
}
public static byte[] download(String groupName, String fileName,
String configRootPath)
throws Exception
{
String classPath = configRootPath == null ? new File(FastDFSUtils.class
.getResource("/").getFile()).getCanonicalPath()
: configRootPath;
String configFilePath = classPath + File.separator + "fdfs_client.conf";
ClientGlobal
.init(configFilePath.replace("%20", " ").replace("\\", "/"));
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
byte[] bytes = null;
bytes = storageClient.download_file(groupName, fileName);
return bytes;
}
}