FastDFSUtil.java 12 KB

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