1. package cn.saiz.drkms.task.crack.utils;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13.  
  14. import org.csource.common.NameValuePair;
  15. import org.csource.fastdfs.ClientGlobal;
  16. import org.csource.fastdfs.FileInfo;
  17. import org.csource.fastdfs.StorageClient;
  18. import org.csource.fastdfs.StorageClient1;
  19. import org.csource.fastdfs.StorageServer;
  20. import org.csource.fastdfs.TrackerClient;
  21. import org.csource.fastdfs.TrackerServer;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24.  
  25. public class FastDFSUtil {
  26.  
  27. public static Logger log = LoggerFactory.getLogger(FastDFSUtil.class);
  28.  
  29. static {
  30. try {
  31. String path = System.getProperty("user.dir") + "/fdfs_client.conf";
  32. File file = new File(path);
  33. if(file == null || !file.exists()){
  34. path = FastDFSUtil.class.getClassLoader()
  35. .getResource("fdfs_client.conf").getPath();
  36. file = new File(path);
  37. if(file == null || !file.exists()){
  38. path = Thread.currentThread().getContextClassLoader().
  39. getResource("fdfs_client.conf").getPath();
  40. }
  41. }
  42. // 读取配置文件
  43. ClientGlobal.init(path);
  44. } catch (Exception e) {
  45. log.error("Init fastdfs error:{}",e.getMessage());
  46. throw new RuntimeException(e);
  47. }
  48. }
  49.  
  50. public static StorageClient1 getClient() throws IOException {
  51. return new StorageClient1(new TrackerClient().getConnection(), null);
  52. }
  53.  
  54. public static StorageClient1 getClient(TrackerServer trackerServer,StorageServer storageServer){
  55. return new StorageClient1(trackerServer, storageServer);
  56. }
  57.  
  58. public static TrackerServer getTrackerServer() throws IOException{
  59. return new TrackerClient().getConnection();
  60. }
  61.  
  62. public static void releaseServerResource(TrackerServer trackerServer,StorageServer storageServer) throws IOException{
  63. if(trackerServer != null) trackerServer.close();
  64. if(storageServer != null) storageServer.close();
  65. }
  66. ///////////////////上传文件////////////////
  67. public static String uploadFile(String filePath)
  68. throws IOException {
  69. return uploadFile(null, new File(filePath));
  70. }
  71.  
  72. public static String uploadFile(String groupName, String filePath)
  73. throws IOException {
  74. return uploadFile(groupName, new File(filePath));
  75. }
  76.  
  77. public static String uploadFile(String groupName, File file)
  78. throws IOException {
  79. if (file == null) return null;
  80. return uploadFile(groupName, new FileInputStream(file), file.getName(), file.length());
  81. }
  82.  
  83. public static String uploadFile(File file) throws IOException {
  84. return uploadFile(null, file);
  85. }
  86.  
  87. public static String uploadFile(InputStream input,String fileName, long fileLength)
  88. throws IOException {
  89. return uploadFile(null, input, fileName, fileLength);
  90. }
  91.  
  92. public static String uploadFile(String groupName, InputStream inStream,
  93. String uploadFileName, long fileLength) throws IOException {
  94. return uploadByteFile(groupName, uploadFileName, fileLength, getFileBuffer(inStream, fileLength));
  95. }
  96.  
  97. public static String uploadByteFile(String groupName,
  98. String fileName, long fileLength, byte[] fileBuff)
  99. throws IOException {
  100. String fileId = null;
  101. String fileExtName = null;
  102. if (fileName.contains(".")) {
  103. fileExtName = fileName.substring(fileName
  104. .lastIndexOf(".") + 1);
  105. } else {
  106. log.warn("Fail to upload file, because the format of filename is illegal.");
  107. return fileId;
  108. }
  109.  
  110. // 建立连接
  111. TrackerServer trackerServer = getTrackerServer();
  112. StorageServer storageServer = null;
  113. StorageClient1 client = getClient(trackerServer, storageServer);
  114.  
  115. // 设置元信息
  116. NameValuePair[] metaList = new NameValuePair[3];
  117. metaList[0] = new NameValuePair("fileName", fileName);
  118. metaList[1] = new NameValuePair("fileExtName", fileExtName);
  119. metaList[2] = new NameValuePair("fileLength",
  120. String.valueOf(fileLength));
  121.  
  122. // 上传文件
  123. try {
  124. fileId = client.upload_file1(groupName, fileBuff, fileExtName,
  125. metaList);
  126. log.info("upload success. file id is: " + fileId);
  127. } catch (Exception e) {
  128. log.warn("Upload file \"" + fileName + "\"fails");
  129. throw new RuntimeException(e);
  130. } finally {
  131. releaseServerResource(trackerServer, storageServer);
  132. }
  133. return fileId;
  134. }
  135. //////////////////上传文件END/////////////////
  136.  
  137. /////////////////下载文件////////////////////
  138. public static byte[] downloadFile(String groupName, String filepath)
  139. throws Exception {
  140. if(filepath == null || "".equals(filepath)) return null;
  141. TrackerServer trackerServer = null;
  142. StorageServer storageServer = null;
  143. try {
  144. TrackerClient tracker = new TrackerClient();
  145. trackerServer = tracker.getConnection();
  146. StorageClient storageClient = new StorageClient(trackerServer,
  147. storageServer);
  148. // StorageClient1 storageClient1 = new StorageClient1(trackerServer,
  149. // storageServer);
  150. return storageClient.download_file(groupName, filepath);
  151. } finally {
  152. releaseServerResource(trackerServer, storageServer);
  153. }
  154. }
  155.  
  156. public static byte[] downloadFile(String fileId) throws Exception {
  157. TrackerServer trackerServer = null;
  158. StorageServer storageServer = null;
  159. try {
  160. trackerServer = getTrackerServer();
  161. StorageClient1 storageClient1 = getClient(trackerServer, storageServer);
  162. return storageClient1.download_file1(fileId);
  163. } finally {
  164. releaseServerResource(trackerServer, storageServer);
  165. }
  166. }
  167.  
  168. public static void downloadFile(String fileId, OutputStream out)
  169. throws Exception {
  170. if(fileId != null && out != null){
  171. try {
  172. byte[] b = downloadFile(fileId);
  173. out.write(b);
  174. out.flush();
  175. } finally {
  176. out.close();
  177. }
  178. }
  179. }
  180.  
  181. public static void downloadFile(String groupName, String filepath,
  182. OutputStream out) throws Exception {
  183. if(filepath != null && out != null){
  184. try {
  185. byte[] b = downloadFile(groupName, filepath);
  186. out.write(b);
  187. out.flush();
  188. } finally {
  189. out.close();
  190. }
  191. }
  192. }
  193.  
  194. public static void downloadFile(String groupName, String filepath,
  195. File descFile) throws Exception {
  196. OutputStream out = null;
  197. try {
  198. out = new FileOutputStream(descFile);
  199. downloadFile(groupName, filepath, out);
  200. } finally {
  201. if(out != null)
  202. out.close();
  203. }
  204. }
  205.  
  206. public static List<Map<String,String>> getFileMate(String groupName, String filepath)
  207. throws Exception {
  208. if(filepath == null) return null;
  209. TrackerServer trackerServer = getTrackerServer();
  210. StorageServer storageServer = null;
  211. StorageClient storageClient = getClient(trackerServer, storageServer);
  212. NameValuePair nvps[] = storageClient.get_metadata(groupName, filepath);
  213. List<Map<String,String>> list = new ArrayList<Map<String,String>>();
  214. for (NameValuePair nvp : nvps) {
  215. Map<String,String> map = new HashMap<String,String>();
  216. map.put(nvp.getName(), nvp.getValue());
  217. list.add(map);
  218. }
  219. return list;
  220. }
  221.  
  222. /**
  223. * 删除文件
  224. * @return 0:删除成功 其他数值失败
  225. */
  226. public static int deleteFile(String groupName, String filepath)
  227. throws Exception {
  228. if(filepath == null) return -1;
  229. TrackerServer trackerServer = getTrackerServer();
  230. StorageServer storageServer = null;
  231. StorageClient storageClient = new StorageClient(trackerServer,
  232. storageServer);
  233. return storageClient.delete_file(groupName, filepath);
  234. }
  235.  
  236. /**
  237. * 删除文件
  238. *
  239. * @param groupName
  240. * @param filepath
  241. * @throws Exception
  242. */
  243. public static int deleteFile(String fileId) throws Exception {
  244. if(fileId == null) return -1;
  245. TrackerServer trackerServer = null;
  246. StorageServer storageServer = null;
  247. try {
  248. trackerServer = getTrackerServer();
  249. StorageClient1 storageClient1 = getClient(trackerServer, storageServer);
  250. return storageClient1.delete_file1(fileId);
  251. } finally {
  252. releaseServerResource(trackerServer, storageServer);
  253. }
  254. }
  255.  
  256. /**
  257. * 通过fileID查询上传的文件信息
  258. *
  259. * @param fileId
  260. * eg:group1/M00/00/00/wKi3glS_XEaAVL3DAAwdkYUdoP8278.gif
  261. * @return
  262. * @throws Exception
  263. */
  264. public static FileInfo getFileInfo(String fileId) throws Exception {
  265. if (fileId == null) return null;
  266. TrackerServer trackerServer = getTrackerServer();
  267. StorageServer storageServer = null;
  268. try {
  269. StorageClient1 client = new StorageClient1(trackerServer, storageServer);
  270. return client.get_file_info1(fileId);
  271. } finally {
  272. releaseServerResource(trackerServer, storageServer);
  273. }
  274. }
  275.  
  276. private static byte[] getFileBuffer(InputStream inStream, long fileLength)
  277. throws IOException {
  278. byte[] buffer = new byte[256 * 1024];
  279. byte[] fileBuffer = new byte[(int) fileLength];
  280.  
  281. int count = 0;
  282. int length = 0;
  283.  
  284. while ((length = inStream.read(buffer)) != -1) {
  285. for (int i = 0; i < length; ++i) {
  286. fileBuffer[count + i] = buffer[i];
  287. }
  288. count += length;
  289. }
  290. return fileBuffer;
  291. }
  292.  
  293. public static void main(String[] args) throws Exception {
  294. List<Map<String, String>> fileMate = getFileMate("group1","M00/98/73/CgMEwVh5M62AQnf-ABa36R1PUuY660.pdf");
  295. System.out.println(fileMate.toString());
  296. }
  297. }

FastDFS java 辅助类的更多相关文章

  1. 关于FastDFS Java客户端源码中的一个不太明白的地方

    下面代码是package org.csource.fastdfs下TrackerGroup.java文件中靠近结束的一段代码,我下载的这个源码的版本是1.24. /** * return connec ...

  2. 使用Java辅助类(CountDownLatch、CyclicBarrier、Semaphore)并发编程

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法 一.C ...

  3. FastDfs java客户端上传、删除文件

    #配置文件 connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 9090 http.an ...

  4. fastdfs java client error

    tracker,storage运行正常,利用fdfs_test程序做测试,可以正常上传下载文件. tracker的端口配置 # HTTP port on this tracker server htt ...

  5. Java辅助类持续汇总~

    /** * 01 * 描述:List<String>集合去除重复数据 * [时间 2019年3月5日下午3:54:09 作者 陶攀峰] */ public static List<S ...

  6. fastDFS 上传 java源码

    要想搭建fastDFS网上有相近的文章: 分布式文件系统 - FastDFS 在 CentOS 下配置安装部署 分布式文件系统 - FastDFS 配置 Nginx 模块及上传测试 首先下载fastd ...

  7. 分享知识-快乐自己:FastDFS 上传 java 源码

    FastDFS 上传 java 源码:点我下载源码 首先导入 POM 文件:解决 maven 不能下载 fastdfs-client-java JAR <dependency> <g ...

  8. Java 客户端操作 FastDFS 实现文件上传下载替换删除

    FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...

  9. 网站文件系统发展&&分布式文件系统fastDFS

    网站文件系统发展 1.单机时代的图片服务器架构 初创时期由于时间紧迫,开发人员水平也很有限等原因.所以通常就直接在website文件所在的目录下,建立1个upload子目录,用于保存用户上传的图片文件 ...

随机推荐

  1. 烂笔头——JAVA/JSP

    学艺不精,一些小零头放这里备忘 Object[] obj = (Object[])list.get(i);//取list的某个项目 jsp中出现out.println( )和System.out.pr ...

  2. Dom4J 解析xml ,类查询

    /** * 从XML文件比对,传入provinceId 返回 provinceShortName * @param provinceid * @return */ public static Stri ...

  3. bzoj 5072 [Lydsy1710月赛]小A的树——树形dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 发现对于每个子树,黑点个数确定时,连通块的大小取值范围一定是一段区间:所以考虑只最小化 ...

  4. bzoj 1086 [SCOI2005]王室联邦——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1086 于是去看了题解. 要回溯的时候再把自己加进栈里判断.这样才能保证剩下的可以通过自己连到 ...

  5. 升级后,使用dynamic报错

    程序升级为.NET 4.0后,在程序中使用dynamic时出现下列错误 错误 5 预定义的类型“Microsoft.CSharp.RuntimeBinder.Binder”未定义或未导入错误 4 找不 ...

  6. dcos下rexray服务的配置

    在dcos环境下,rexray服务的默认配置文件为/opt/mesosphere/etc/rexray.conf,而其服务文件则是 /etc/systemd/system/dcos-rexray.se ...

  7. 四、Chrome开发者工具详解(4)-Profiles面板

    摘自: http://www.cnblogs.com/charliechu/p/6003713.html

  8. 用Merge存储引擎中间件实现MySQL分表

    觉得一个用Merge存储引擎中间件来实现MySQL分表的方法不错. 可以看下这个博客写的很清楚--> http://www.cnblogs.com/xbq8080/p/6628034.html ...

  9. 最新sublimetext3080注册

    ----- BEGIN LICENSE -----K-20Single User LicenseEA7E-9401293A099EC1 C0B5C7C5 33EBF0CF BE82FE3BEAC216 ...

  10. Algorithms : Programming Assignment 3: Pattern Recognition

    Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...