FastDFS java 辅助类
package cn.saiz.drkms.task.crack.utils; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class FastDFSUtil { public static Logger log = LoggerFactory.getLogger(FastDFSUtil.class); static {
try {
String path = System.getProperty("user.dir") + "/fdfs_client.conf";
File file = new File(path);
if(file == null || !file.exists()){
path = FastDFSUtil.class.getClassLoader()
.getResource("fdfs_client.conf").getPath();
file = new File(path);
if(file == null || !file.exists()){
path = Thread.currentThread().getContextClassLoader().
getResource("fdfs_client.conf").getPath();
}
}
// 读取配置文件
ClientGlobal.init(path);
} catch (Exception e) {
log.error("Init fastdfs error:{}",e.getMessage());
throw new RuntimeException(e);
}
} public static StorageClient1 getClient() throws IOException {
return new StorageClient1(new TrackerClient().getConnection(), null);
} public static StorageClient1 getClient(TrackerServer trackerServer,StorageServer storageServer){
return new StorageClient1(trackerServer, storageServer);
} public static TrackerServer getTrackerServer() throws IOException{
return new TrackerClient().getConnection();
} public static void releaseServerResource(TrackerServer trackerServer,StorageServer storageServer) throws IOException{
if(trackerServer != null) trackerServer.close();
if(storageServer != null) storageServer.close();
}
///////////////////上传文件////////////////
public static String uploadFile(String filePath)
throws IOException {
return uploadFile(null, new File(filePath));
} public static String uploadFile(String groupName, String filePath)
throws IOException {
return uploadFile(groupName, new File(filePath));
} public static String uploadFile(String groupName, File file)
throws IOException {
if (file == null) return null;
return uploadFile(groupName, new FileInputStream(file), file.getName(), file.length());
} public static String uploadFile(File file) throws IOException {
return uploadFile(null, file);
} public static String uploadFile(InputStream input,String fileName, long fileLength)
throws IOException {
return uploadFile(null, input, fileName, fileLength);
} public static String uploadFile(String groupName, InputStream inStream,
String uploadFileName, long fileLength) throws IOException {
return uploadByteFile(groupName, uploadFileName, fileLength, getFileBuffer(inStream, fileLength));
} public static String uploadByteFile(String groupName,
String fileName, long fileLength, byte[] fileBuff)
throws IOException {
String fileId = null;
String fileExtName = null;
if (fileName.contains(".")) {
fileExtName = fileName.substring(fileName
.lastIndexOf(".") + 1);
} else {
log.warn("Fail to upload file, because the format of filename is illegal.");
return fileId;
} // 建立连接
TrackerServer trackerServer = getTrackerServer();
StorageServer storageServer = null;
StorageClient1 client = getClient(trackerServer, storageServer); // 设置元信息
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName", fileName);
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength",
String.valueOf(fileLength)); // 上传文件
try {
fileId = client.upload_file1(groupName, fileBuff, fileExtName,
metaList);
log.info("upload success. file id is: " + fileId);
} catch (Exception e) {
log.warn("Upload file \"" + fileName + "\"fails");
throw new RuntimeException(e);
} finally {
releaseServerResource(trackerServer, storageServer);
}
return fileId;
}
//////////////////上传文件END///////////////// /////////////////下载文件////////////////////
public static byte[] downloadFile(String groupName, String filepath)
throws Exception {
if(filepath == null || "".equals(filepath)) return null;
TrackerServer trackerServer = null;
StorageServer storageServer = null;
try {
TrackerClient tracker = new TrackerClient();
trackerServer = tracker.getConnection();
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
// StorageClient1 storageClient1 = new StorageClient1(trackerServer,
// storageServer);
return storageClient.download_file(groupName, filepath);
} finally {
releaseServerResource(trackerServer, storageServer);
}
} public static byte[] downloadFile(String fileId) throws Exception {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
try {
trackerServer = getTrackerServer();
StorageClient1 storageClient1 = getClient(trackerServer, storageServer);
return storageClient1.download_file1(fileId);
} finally {
releaseServerResource(trackerServer, storageServer);
}
} public static void downloadFile(String fileId, OutputStream out)
throws Exception {
if(fileId != null && out != null){
try {
byte[] b = downloadFile(fileId);
out.write(b);
out.flush();
} finally {
out.close();
}
}
} public static void downloadFile(String groupName, String filepath,
OutputStream out) throws Exception {
if(filepath != null && out != null){
try {
byte[] b = downloadFile(groupName, filepath);
out.write(b);
out.flush();
} finally {
out.close();
}
}
} public static void downloadFile(String groupName, String filepath,
File descFile) throws Exception {
OutputStream out = null;
try {
out = new FileOutputStream(descFile);
downloadFile(groupName, filepath, out);
} finally {
if(out != null)
out.close();
}
} public static List<Map<String,String>> getFileMate(String groupName, String filepath)
throws Exception {
if(filepath == null) return null;
TrackerServer trackerServer = getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = getClient(trackerServer, storageServer);
NameValuePair nvps[] = storageClient.get_metadata(groupName, filepath);
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
for (NameValuePair nvp : nvps) {
Map<String,String> map = new HashMap<String,String>();
map.put(nvp.getName(), nvp.getValue());
list.add(map);
}
return list;
} /**
* 删除文件
* @return 0:删除成功 其他数值失败
*/
public static int deleteFile(String groupName, String filepath)
throws Exception {
if(filepath == null) return -1;
TrackerServer trackerServer = getTrackerServer();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
return storageClient.delete_file(groupName, filepath);
} /**
* 删除文件
*
* @param groupName
* @param filepath
* @throws Exception
*/
public static int deleteFile(String fileId) throws Exception {
if(fileId == null) return -1;
TrackerServer trackerServer = null;
StorageServer storageServer = null;
try {
trackerServer = getTrackerServer();
StorageClient1 storageClient1 = getClient(trackerServer, storageServer);
return storageClient1.delete_file1(fileId);
} finally {
releaseServerResource(trackerServer, storageServer);
}
} /**
* 通过fileID查询上传的文件信息
*
* @param fileId
* eg:group1/M00/00/00/wKi3glS_XEaAVL3DAAwdkYUdoP8278.gif
* @return
* @throws Exception
*/
public static FileInfo getFileInfo(String fileId) throws Exception {
if (fileId == null) return null;
TrackerServer trackerServer = getTrackerServer();
StorageServer storageServer = null;
try {
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
return client.get_file_info1(fileId);
} finally {
releaseServerResource(trackerServer, storageServer);
}
} private static byte[] getFileBuffer(InputStream inStream, long fileLength)
throws IOException {
byte[] buffer = new byte[256 * 1024];
byte[] fileBuffer = new byte[(int) fileLength]; int count = 0;
int length = 0; while ((length = inStream.read(buffer)) != -1) {
for (int i = 0; i < length; ++i) {
fileBuffer[count + i] = buffer[i];
}
count += length;
}
return fileBuffer;
} public static void main(String[] args) throws Exception {
List<Map<String, String>> fileMate = getFileMate("group1","M00/98/73/CgMEwVh5M62AQnf-ABa36R1PUuY660.pdf");
System.out.println(fileMate.toString());
}
}
FastDFS java 辅助类的更多相关文章
- 关于FastDFS Java客户端源码中的一个不太明白的地方
下面代码是package org.csource.fastdfs下TrackerGroup.java文件中靠近结束的一段代码,我下载的这个源码的版本是1.24. /** * return connec ...
- 使用Java辅助类(CountDownLatch、CyclicBarrier、Semaphore)并发编程
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法 一.C ...
- FastDfs java客户端上传、删除文件
#配置文件 connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 9090 http.an ...
- fastdfs java client error
tracker,storage运行正常,利用fdfs_test程序做测试,可以正常上传下载文件. tracker的端口配置 # HTTP port on this tracker server htt ...
- Java辅助类持续汇总~
/** * 01 * 描述:List<String>集合去除重复数据 * [时间 2019年3月5日下午3:54:09 作者 陶攀峰] */ public static List<S ...
- fastDFS 上传 java源码
要想搭建fastDFS网上有相近的文章: 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试 首先下载fastd ...
- 分享知识-快乐自己:FastDFS 上传 java 源码
FastDFS 上传 java 源码:点我下载源码 首先导入 POM 文件:解决 maven 不能下载 fastdfs-client-java JAR <dependency> <g ...
- Java 客户端操作 FastDFS 实现文件上传下载替换删除
FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...
- 网站文件系统发展&&分布式文件系统fastDFS
网站文件系统发展 1.单机时代的图片服务器架构 初创时期由于时间紧迫,开发人员水平也很有限等原因.所以通常就直接在website文件所在的目录下,建立1个upload子目录,用于保存用户上传的图片文件 ...
随机推荐
- Python:循环
循环语句:while循环,for循环 例1:求1-100的所有数的和 n = 100 sum = 0 count = 1 while count <= n: sum = sum + count ...
- 51nod1674:区间的价值2(分治,利用&和|的收敛性)
lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积. 例如3个数2,3,6.它们and起来的值为2,or起来的值为7,这个区间对答案的贡献为2 ...
- BZOJ_3123_[Sdoi2013]森林_主席树+启发式合并
BZOJ_3123_[Sdoi2013]森林_主席树+启发式合并 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20 ...
- 「IOI1998」「LuoguP4342」Polygon(区间dp
P4342 [IOI1998]Polygon - 洛谷 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符 ...
- flask logger
Flask uses standard Python logging. All Flask-related messages are logged under the 'flask' logger n ...
- Can you answer these queries II
题意: 给一长度为n的序列,有m组询问,每一组询问给出[l,r]询问区间内的最大去重连续子段和. 解法: 考虑一下简化后的问题:如果题目要求询问查询以$r$为右端点且$l$大于等于给定值的去重连续子段 ...
- 网页元素定位Position
第九章: 网页元素定位Position position属性 static:默认值,没有定位 relative:相对定位 absolute:绝对定位 fixed:固定定位 (一般不用) stati ...
- TypeScript完全解读(26课时)_9.TypeScript完全解读-TS中的类
9.TypeScript完全解读-TS中的类 创建class.ts文件,并在index.ts内引用 创建一个类,这个类在创建好后有好几个地方都标红了 这是tslint的一些验证规则 一保存就会自动修复 ...
- UVa 12661 Funny Car Racing (dijkstra)
题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用d ...
- jquery 点击某一行,得到这一行的每个列的数据
<html><head> <title>test</title> <script src="../Scripts/jquery-1.8. ...