FastDFS java 辅助类
2024-09-08 13:42:01
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());
}
}
最新文章
- CSS样式之优先级
- css随记01编辑技巧,背景与边框
- 3分钟wamp中php安装 pear 然而并没有用 并没能借此安装phpunit 不得不借用了其他的方式安装phpunit
- yiii 框架登录 判断是否是游客模式及未登录状态
- 埃及分数-IDA*
- Selenium2+python自动化24-js处理富文本(带iframe)
- JS思维之路菜鸟也能有大能量(2)--模拟数组合并concat
- 2016年11月13日 星期日 --出埃及记 Exodus 20:4
- Linux内核加载全流程
- PM【terminal】
- php 原生能力进阶
- jQuery教程详解(一)
- kali使用Fluxion钓鱼WiFi
- Android下资源使用的方式-android学习之旅(五十三)
- Beamer制作索引
- windows 2008远程桌面企业协议号
- windows Maven3.0 服务器配置搭建
- DevExpress 行事历(Scheduler)的常用属性、事件和方法
- is_array判断是否为数组
- angular开发中的两大问题