FastDFS实现文件上传下载的工具类详细步骤

前言 要了解FastDFS的工作流程

1、首先加入jar包


  
  1. <dependency>
  2. <groupId>net.oschina.zcx7878</groupId>
  3. <artifactId>fastdfs-client-java</artifactId>
  4. <version>1.27.0.0</version>
  5. </dependency>

2、配置FastDFS基本信息:

     在项目的resource目录下面新建fdfs_client.conf配置文件,配置信息如下:


  
  1. connect_timeout = 60
  2. network_timeout = 60
  3. charset = UTF-8
  4. http.tracker_http_port = 8080
  5. tracker_server = 192.168.200.128:22122

3、封装文件的信息(FastDFSFile.java)


  
  1. public class FastDFSFile {
  2. //文件名字
  3. private String name;
  4. //文件内容
  5. private byte[] content;
  6. //文件扩展名
  7. private String ext;
  8. //文件MD5摘要值
  9. private String md5;
  10. //文件创建作者
  11. private String author;
  12. public FastDFSFile(String name, byte[] content, String ext, String height,
  13. String width, String author) {
  14. super();
  15. this.name = name;
  16. this.content = content;
  17. this.ext = ext;
  18. this.author = author;
  19. }
  20. public FastDFSFile(String name, byte[] content, String ext) {
  21. super();
  22. this.name = name;
  23. this.content = content;
  24. this.ext = ext;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public byte[] getContent() {
  33. return content;
  34. }
  35. public void setContent(byte[] content) {
  36. this.content = content;
  37. }
  38. public String getExt() {
  39. return ext;
  40. }
  41. public void setExt(String ext) {
  42. this.ext = ext;
  43. }
  44. public String getMd5() {
  45. return md5;
  46. }
  47. public void setMd5(String md5) {
  48. this.md5 = md5;
  49. }
  50. public String getAuthor() {
  51. return author;
  52. }
  53. public void setAuthor(String author) {
  54. this.author = author;
  55. }
  56. }

4、编写文件操作的工具类(上传、下载和删除等等操作)


  
  1. import org.csource.common.NameValuePair;
  2. import org.csource.fastdfs.*;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.core.io.ClassPathResource;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. public class FastDFSClient {
  9. private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
  10. /***
  11. * 初始化加载FastDFS的TrackerServer配置
  12. */
  13. static {
  14. try {
  15. String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
  16. ClientGlobal.init(filePath);
  17. } catch (Exception e) {
  18. logger.error("FastDFS Client Init Fail!",e);
  19. }
  20. }
  21. /***
  22. * 文件上传
  23. * @param file
  24. * @return 1.文件的组名 2.文件的路径信息
  25. */
  26. public static String[] upload(FastDFSFile file) {
  27. //获取文件的作者
  28. NameValuePair[] meta_list = new NameValuePair[1];
  29. meta_list[0] = new NameValuePair("author", file.getAuthor());
  30. //接收返回数据
  31. String[] uploadResults = null;
  32. StorageClient storageClient=null;
  33. try {
  34. //创建StorageClient客户端对象
  35. storageClient = getTrackerClient();
  36. /***
  37. * 文件上传
  38. * 1)文件字节数组
  39. * 2)文件扩展名
  40. * 3)文件作者
  41. */
  42. uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
  43. } catch (Exception e) {
  44. logger.error("Exception when uploadind the file:" + file.getName(), e);
  45. }
  46. if (uploadResults == null && storageClient!=null) {
  47. logger.error("upload file fail, error code:" + storageClient.getErrorCode());
  48. }
  49. //获取组名
  50. String groupName = uploadResults[0];
  51. //获取文件存储路径
  52. String remoteFileName = uploadResults[1];
  53. return uploadResults;
  54. }
  55. /***
  56. * 获取文件信息
  57. * @param groupName:组名
  58. * @param remoteFileName:文件存储完整名
  59. * @return
  60. */
  61. public static FileInfo getFile(String groupName, String remoteFileName) {
  62. try {
  63. StorageClient storageClient = getTrackerClient();
  64. return storageClient.get_file_info(groupName, remoteFileName);
  65. } catch (Exception e) {
  66. logger.error("Exception: Get File from Fast DFS failed", e);
  67. }
  68. return null;
  69. }
  70. /***
  71. * 文件下载
  72. * @param groupName
  73. * @param remoteFileName
  74. * @return
  75. */
  76. public static InputStream downFile(String groupName, String remoteFileName) {
  77. try {
  78. //创建StorageClient
  79. StorageClient storageClient = getTrackerClient();
  80. //下载文件
  81. byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
  82. InputStream ins = new ByteArrayInputStream(fileByte);
  83. return ins;
  84. } catch (Exception e) {
  85. logger.error("Exception: Get File from Fast DFS failed", e);
  86. }
  87. return null;
  88. }
  89. /***
  90. * 文件删除
  91. * @param groupName
  92. * @param remoteFileName
  93. * @throws Exception
  94. */
  95. public static void deleteFile(String groupName, String remoteFileName)
  96. throws Exception {
  97. //创建StorageClient
  98. StorageClient storageClient = getTrackerClient();
  99. //删除文件
  100. int i = storageClient.delete_file(groupName, remoteFileName);
  101. }
  102. /***
  103. * 获取Storage组
  104. * @param groupName
  105. * @return
  106. * @throws IOException
  107. */
  108. public static StorageServer[] getStoreStorages(String groupName)
  109. throws IOException {
  110. //创建TrackerClient
  111. TrackerClient trackerClient = new TrackerClient();
  112. //获取TrackerServer
  113. TrackerServer trackerServer = trackerClient.getConnection();
  114. //获取Storage组
  115. return trackerClient.getStoreStorages(trackerServer, groupName);
  116. }
  117. /***
  118. * 获取Storage信息,IP和端口
  119. * @param groupName
  120. * @param remoteFileName
  121. * @return
  122. * @throws IOException
  123. */
  124. public static ServerInfo[] getFetchStorages(String groupName,
  125. String remoteFileName) throws IOException {
  126. TrackerClient trackerClient = new TrackerClient();
  127. TrackerServer trackerServer = trackerClient.getConnection();
  128. return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
  129. }
  130. /***
  131. * 获取Tracker服务地址
  132. * @return
  133. * @throws IOException
  134. */
  135. public static String getTrackerUrl() throws IOException {
  136. return "http://"+getTrackerServer().getInetSocketAddress().getHostString()+":"+ClientGlobal.getG_tracker_http_port()+"/";
  137. }
  138. /***
  139. * 获取Storage客户端
  140. * @return
  141. * @throws IOException
  142. */
  143. private static StorageClient getTrackerClient() throws IOException {
  144. TrackerServer trackerServer = getTrackerServer();
  145. StorageClient storageClient = new StorageClient(trackerServer, null);
  146. return storageClient;
  147. }
  148. /***
  149. * 获取Tracker
  150. * @return
  151. * @throws IOException
  152. */
  153. private static TrackerServer getTrackerServer() throws IOException {
  154. TrackerClient trackerClient = new TrackerClient();
  155. TrackerServer trackerServer = trackerClient.getConnection();
  156. return trackerServer;
  157. }
  158. }

 

文章来源: blog.csdn.net,作者:轻狂书生FS,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/LookForDream_/article/details/104521309

(完)