FastDFSUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.yihu.wlyy.util.fastdfs;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.InetSocketAddress;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.fasterxml.jackson.databind.node.ObjectNode;
  8. import org.csource.common.MyException;
  9. import org.csource.common.NameValuePair;
  10. import org.csource.fastdfs.ClientGlobal;
  11. import org.csource.fastdfs.ProtoCommon;
  12. import org.csource.fastdfs.StorageClient;
  13. import org.csource.fastdfs.StorageClient1;
  14. import org.csource.fastdfs.TrackerServer;
  15. /**
  16. * FastDFS 客户端工具.
  17. *
  18. * @author szx
  19. */
  20. public class FastDFSUtil {
  21. public final static String GroupField = "groupName";
  22. public final static String RemoteFileField = "remoteFileName";
  23. public final static String FileIdField = "fid";
  24. public final static String FileUrlField = "fileUrl";
  25. // static TrackerClient tracker;
  26. // static TrackerServer trackerServer;
  27. // static StorageServer storageServer;
  28. // static StorageClient client;
  29. //
  30. // static {
  31. // try {
  32. // XEnvironmentOption environmentOption = ServiceFactory.getService(Services.EnvironmentOption);
  33. // String basePath = FastDFSUtil.class.getResource("/").getPath();
  34. // String configFile = basePath + environmentOption.getOption(EnvironmentOptions.FastDFSConfig);
  35. //
  36. // ClientGlobal.init(configFile);
  37. //
  38. // tracker = new TrackerClient();
  39. // trackerServer = tracker.getConnection();
  40. // storageServer = null;
  41. // client = new StorageClient(trackerServer, storageServer);
  42. // } catch (FileNotFoundException e) {
  43. // LogService.getLogger(FastDFSUtil.class).fatal("FastDFS配置文件打开失败: " + e.getMessage());
  44. // } catch (IOException e) {
  45. // LogService.getLogger(FastDFSUtil.class).fatal("FastDFS初始化失败: " + e.getMessage());
  46. // } catch (MyException e) {
  47. // LogService.getLogger(FastDFSUtil.class).fatal("FastDFS初始化失败: " + e.getMessage());
  48. // }
  49. // }
  50. final static int BUFFER_SIZE = 4096;
  51. /**
  52. * 以输入流的方式上传文件
  53. * InputStream in = new FileInputStream("C://Desert.jpg");
  54. * ObjectNode msg = FileUtil.upload(in,"jpg", "沙漠");
  55. * in.close();
  56. *
  57. * @param in 输入流
  58. * @param fileExtension 文件扩展名,不要带“.”
  59. * @param description 文件名称(中文)
  60. * @return 返回值的格式如下:
  61. * {
  62. * "groupName": "healthArchiveGroup",
  63. * "remoteFileName": "/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
  64. * "fid": "group1/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
  65. * "fileURL": "http://172.19.103.13/healthArchiveGroup/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg"
  66. * }
  67. *
  68. * groupName 及 remoteFileName 可以用于查询在 fastDFS 中文件的信息,如果只是图片显示,可以忽略这两个值。
  69. * fid 保存了在 fastDFS 上的完整路径,为了避免将来服务器域名发生变更,最好使用本值.服务器的域名另外配置。
  70. * fileURL 保存了完整的 web 访问路径,为了避免将来服务器域名发生变更,最好不要直接使用本值。
  71. * 如果需要在下载时,可以显示原始文件名,请在访问file_url时,增加 attname 参数,如:
  72. *
  73. * http://host/healthArchiveGroup/M00/00/00/rBFuH1XdIseAUTZZAA1rIuRd3Es062.jpg?attname=a.jpg
  74. *
  75. * @throws Exception
  76. */
  77. public static ObjectNode upload(InputStream in, String fileExtension,
  78. String description) throws Exception {
  79. StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
  80. try {
  81. NameValuePair[] fileMetaData;
  82. fileMetaData = new NameValuePair[1];
  83. fileMetaData[0] = new NameValuePair("description", description == null ? "" : description);
  84. ObjectMapper objectMapper = new ObjectMapper();
  85. ObjectNode message = objectMapper.createObjectNode();
  86. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  87. byte[] buff = new byte[BUFFER_SIZE]; //buff用于存放循环读取的临时数据
  88. int rc = 0;
  89. while ((rc = in.read(buff, 0, BUFFER_SIZE)) > 0) {
  90. swapStream.write(buff, 0, rc);
  91. }
  92. byte[] fileBuffer = swapStream.toByteArray(); //in_b为转换之后的结果
  93. TrackerServer trackerServer = FastDFSClientPool.getInstance().getTrackerServer();
  94. String[] results = client.upload_file(fileBuffer, fileExtension, fileMetaData);
  95. if (results != null) {
  96. String fileId;
  97. int ts;
  98. String token;
  99. String fileURl;
  100. InetSocketAddress socketAddress;
  101. String groupName = results[0];
  102. String remoteFile = results[1];
  103. message.put(GroupField, groupName);
  104. message.put(RemoteFileField, remoteFile);
  105. fileId = groupName + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remoteFile;
  106. message.put(FileIdField, fileId);
  107. socketAddress = trackerServer.getInetSocketAddress();
  108. fileURl = "http://" + socketAddress.getAddress().getHostAddress();
  109. if (ClientGlobal.g_tracker_http_port != 80) {
  110. fileURl += ":" + ClientGlobal.g_tracker_http_port;
  111. }
  112. fileURl += "/" + fileId;
  113. if (ClientGlobal.g_anti_steal_token) {
  114. ts = (int) (System.currentTimeMillis() / 1000);
  115. token = ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key);
  116. fileURl += "?token=" + token + "&ts=" + ts;
  117. }
  118. message.put(FileUrlField, fileURl);
  119. return message;
  120. } else {
  121. return null;
  122. }
  123. }finally {
  124. FastDFSClientPool.getInstance().releaseStorageClient(client);
  125. }
  126. }
  127. /**
  128. * 上传本地文件
  129. * ObjectNode a = FileUtil.upload("C://Desert.jpg", "沙漠");
  130. * System.out.println(a.toString());
  131. *
  132. * @param fileName 本地文件的绝对路径,如 C://Desert.jpg
  133. * @param description 文件备注, 可以为空
  134. * @return {"groupName":"group1","remoteFileName":"/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg"
  135. * {
  136. * "groupName": "healthArchiveGroup",
  137. * "remoteFileName": "/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
  138. * "fid": "group1/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
  139. * "fileURL": "http://172.19.103.13/healthArchiveGroup/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg"
  140. * }
  141. *
  142. * groupName 及 remoteFileName 可以用于查询在 fastDFS 中文件的信息,如果只是图片显示,可以忽略这两个值。
  143. * fid 保存了在 fastDFS 上的完整路径,为了避免将来服务器域名发生变更,最好使用本值.服务器的域名另外配置。
  144. * fileURL 保存了完整的 web 访问路径,为了避免将来服务器域名发生变更,最好不要直接使用本值。
  145. * 如果需要在下载时,可以显示原始文件名,请在访问file_url时,增加 attname 参数,如:
  146. *
  147. * http://host/healthArchiveGroup/M00/00/00/rBFuH1XdIseAUTZZAA1rIuRd3Es062.jpg?attname=a.jpg
  148. *
  149. * @throws Exception
  150. */
  151. public static ObjectNode upload(String fileName, String description) throws Exception {
  152. StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
  153. try {
  154. NameValuePair[] meta_list;
  155. meta_list = new NameValuePair[1];
  156. meta_list[0] = new NameValuePair("description", description == null ? "" : description);
  157. ObjectMapper objectMapper = new ObjectMapper();
  158. ObjectNode message = objectMapper.createObjectNode();
  159. String fileExtName = "";
  160. if (fileName.contains(".")) {
  161. fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1);
  162. } else {
  163. throw new RuntimeException("上传失败, 文件缺失扩展名.");
  164. }
  165. TrackerServer trackerServer = FastDFSClientPool.getInstance().getTrackerServer();
  166. String[] results = client.upload_file(fileName, fileExtName, meta_list);
  167. if (results != null) {
  168. String fileId;
  169. int ts;
  170. String token;
  171. String fileUrl;
  172. InetSocketAddress inetSockAddr;
  173. String groupName = results[0];
  174. String remoteFileName = results[1];
  175. message.put(GroupField, groupName);
  176. message.put(RemoteFileField, remoteFileName);
  177. fileId = groupName + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remoteFileName;
  178. message.put(FileIdField, fileId);
  179. inetSockAddr = trackerServer.getInetSocketAddress();
  180. fileUrl = "http://" + inetSockAddr.getAddress().getHostAddress();
  181. if (ClientGlobal.g_tracker_http_port != 80) {
  182. fileUrl += ":" + ClientGlobal.g_tracker_http_port;
  183. }
  184. fileUrl += "/" + fileId;
  185. if (ClientGlobal.g_anti_steal_token) {
  186. ts = (int) (System.currentTimeMillis() / 1000);
  187. token = ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key);
  188. fileUrl += "?token=" + token + "&ts=" + ts;
  189. }
  190. message.put(FileUrlField, fileUrl);
  191. return message;
  192. } else {
  193. return null;
  194. }
  195. }finally {
  196. FastDFSClientPool.getInstance().releaseStorageClient(client);
  197. }
  198. }
  199. /**
  200. * 下载文件, 返回文件字节数组.
  201. *
  202. * @param groupName 在fastdfs上的卷名
  203. * @param remoteFileName 在fastdfs上的路径
  204. * @return 文件的字节码
  205. * @throws Exception
  206. */
  207. public static byte[] download(String groupName, String remoteFileName) throws Exception {
  208. StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
  209. try {
  210. byte[] b = client.download_file(groupName, remoteFileName);
  211. return b;
  212. }
  213. finally {
  214. FastDFSClientPool.getInstance().releaseStorageClient(client);
  215. }
  216. }
  217. /**
  218. * 下载文件到本地路径上.
  219. *
  220. * @param groupName 在 fastDFS 上的卷名
  221. * @param remoteFileName 在 fastDFS 上的路径
  222. * @param localPath 本地路径
  223. *
  224. * @return 是否下载成功
  225. */
  226. public static String download(String groupName, String remoteFileName, String localPath) throws IOException, MyException {
  227. StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
  228. try {
  229. String localFileName = localPath + "\\" + remoteFileName.replaceAll("/", "_");
  230. client.download_file(groupName, remoteFileName, 0, 0, localFileName);
  231. return localFileName;
  232. }finally {
  233. FastDFSClientPool.getInstance().releaseStorageClient(client);
  234. }
  235. }
  236. /**
  237. * 删除文件。
  238. *
  239. * @param groupName
  240. * @param remoteFileName
  241. */
  242. public static void delete(String groupName, String remoteFileName) throws IOException, MyException {
  243. StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
  244. try {
  245. client.delete_file(groupName, remoteFileName);
  246. }finally {
  247. FastDFSClientPool.getInstance().releaseStorageClient(client);
  248. }
  249. }
  250. }