FastDFS:Java客户都实现⽂件的上传、下载、修改、删除客户端版本:fastdfs_client_v1.24.jar
配置⽂件
connect_timeout = 200
network_timeout = 3000
charset = UTF-8
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
#tracker_server = 192.168.1.171:22122
##集模式俩个跟踪器:
tracker_server = 192.168.1.173:22122
tracker_server = 192.168.1.174:22122
客户端操作:
package util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apachemons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.csourcemon.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerGroup;
import org.csource.fastdfs.TrackerServer;
/**
*
* <B>系统名称:</B><BR>
* <B>模块名称:</B><BR>
* <B>中⽂类名:</B>FastDFS分布式⽂件系统操作客户端<BR>
* <B>概要说明:</B>FastDFS分布式⽂件系统操作客户端<BR>
* @author bhz
* @since 2013年11⽉10⽇
*/
public class FastDFSClientUtils {
private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("f").getPath();
private static Logger logger = Logger(FastDFSClientUtils.class);
private static TrackerClient trackerClient;
//加载⽂件
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
trackerClient = new TrackerClient(trackerGroup);
} catch (Exception e) {
<(e);
}
}
/**
* <B>⽅法名称:</B>上传⽅法<BR>
* <B>概要说明:</B><BR>
* @param file ⽂件
* @param path 路径
* @return上传成功返回id,失败返回null
*/
public static String upload(File file, String path) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
FileInputStream fis = null;
try {
NameValuePair[] meta_list = null; // new NameValuePair[0];
fis = new FileInputStream(file);
byte[] file_buff = null;
if (fis != null) {
int len = fis.available();
file_buff = new byte[len];
}
trackerServer = Connection();
if (trackerServer == null) {
<("getConnection return null");
}
storageServer = StoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);
return fileid;
} catch (Exception ex) {
<(ex);
return null;
}finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
<(e);
}
}
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* <B>⽅法名称:</B>上传⽅法<BR>
* <B>概要说明:</B><BR>
* @param data 数据
* @param path 路径
* @return上传成功返回id,失败返回null
*/
public static String upload(byte[] data, String extName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
NameValuePair[] meta_list = null; // new NameValuePair[0];
trackerServer = Connection();
if (trackerServer == null) {
<("getConnection return null");
}
storageServer = StoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(data, extName, meta_list);
return fileid;
} catch (Exception ex) {
<(ex);
return null;
}finally{
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* <B>⽅法名称:</B>下载⽅法<BR>
* <B>概要说明:</B>通过⽂件id进⾏下载<BR>
* @param fileId ⽂件id
* @return返回InputStream
*/
public static InputStream download(String groupName, String fileId) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
trackerServer = Connection();
if (trackerServer == null) {
<("getConnection return null");
}
storageServer = StoreStorage(trackerServer, groupName); storageClient1 = new StorageClient1(trackerServer, storageServer);
byte[] bytes = storageClient1.download_file1(fileId);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
<(ex);
return null;
} finally {
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* <B>⽅法名称:</B>删除⽅法<BR>
* <B>概要说明:</B>根据id来删除⼀个⽂件<BR>
* @param fileId ⽂件id
* @return删除成功返回0,⾮0则操作失败,返回错误代码
*/
public static int delete(String groupName, String fileId) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
trackerServer = Connection();
if (trackerServer == null) {
<("getConnection return null");
}
storageServer = StoreStorage(trackerServer, groupName); storageClient1 = new StorageClient1(trackerServer, storageServer);
int result = storageClient1.delete_file1(fileId);
connect下载return result;
} catch (Exception ex) {
<(ex);
return 0;
} finally {
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* <B>⽅法名称:</B><BR>
* <B>概要说明:</B><BR>
* @param oldFileId 旧⽂件id
* @param file 新⽂件
* @param path 新⽂件路径
* @return上传成功返回id,失败返回null
*/
public static String modify(String oldGroupName, String oldFileId, File file, String path) {
String fileid = null;
try {
// 先上传
fileid = upload(file, path);
if (fileid == null) {
return null;
}
// 再删除
int delResult = delete(oldGroupName, oldFileId);
if (delResult != 0) {
return null;
}
} catch (Exception ex) {
<(ex);
return null;
}
return fileid;
}
/**
* <B>⽅法名称:</B>获取⽂件后缀名<BR>
* <B>概要说明:</B>获取⽂件后缀名<BR>
* @param fileName
* @return如:"jpg"、"txt"、"zip" 等
*/
private static String getFileExt(String fileName) {
if (StringUtils.isBlank(fileName) || !ains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
}
测试:
package util;
import java.io.File;
import java.io.InputStream;
import org.apachemons.io.FileUtils;
public class FastDFSTest {
/**
* 上传
*/
public static void upload() throws Exception {
// id: group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
// fastdfsBasePath = 192.168.1.170/fastdfs
// url: 192.168.1.170/fastdfs/group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
String path = Property("user.dir") + File.separatorChar + "source" + File.separatorChar + "001.jpg"; File file = new File(path);
String fileId = FastDFSClientUtils.upload(file, path);
System.out.println("本地⽂件:" + path + ",上传成功!⽂件ID为:" + fileId);
}
/**
* 下载
*/
public static void download() throws Exception {
// id: group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
// url: 192.168.1.170/fastdfs/group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg
String fileId = "group1/M00/00/00/wKgBr1crUIuAJ22iAADHTIxNXeI872.jpg";
InputStream inputStream = FastDFSClientUtils.download("group1", fileId);
System.out.println(inputStream.available());
String path = Property("user.dir") + File.separatorChar + "receive" + File.separatorChar + "001.jpg"; System.out.println(path);
//pyInputStreamToFile(inputStream, new File(path));
}
/**
* 删除
*/
public static void delete() throws Exception {
String fileId = "group1/M00/00/00/wKgBr1crVnSAbI4sAAAeV2aU4jU126.jpg";
int result = FastDFSClientUtils.delete("group1", fileId);
System.out.println(result == 0 ? "删除成功" : "删除失败:" + result);
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//upload();
download();
Thread.sleep(10000);
download();
Thread.sleep(10000);
download();
//delete();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论