FastDFSConfig.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package io.renren.config;
  2. import org.csource.common.MyException;
  3. import org.csource.fastdfs.ClientGlobal;
  4. import org.csource.fastdfs.TrackerClient;
  5. import org.csource.fastdfs.TrackerGroup;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.cache.annotation.EnableCaching;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import java.net.InetSocketAddress;
  11. import java.util.List;
  12. /**
  13. * Created by Glenn on 2017/5/18 0018.
  14. */
  15. @Configuration("FastDFSConfig")
  16. @EnableCaching//启用缓存的意思
  17. public class FastDFSConfig {
  18. /***
  19. * Example
  20. @Autowired
  21. private TrackerClient client;
  22. 上传至FastDFS
  23. StorageClient storageClient = new StorageClient(client.getConnection(), null);
  24. String[] ret = storageClient.upload_file(fileContent,type.getPureSuffix(), null);
  25. 下载从FastDFS
  26. StorageClient storageClient = new StorageClient(client.getConnection(), null);
  27. FileInfo fi = storageClient.get_file_info(entity.getGroupName(), entity.getFileUri());
  28. if (fi == null) {
  29. throw new Exception("File information from FastDFS is null");
  30. }
  31. byte[] fileContent = storageClient.download_file(entity.getGroupName(), entity.getFileUri());
  32. if (fileContent == null) {
  33. throw new Exception("File entity from FastDFS is null");
  34. }
  35. * */
  36. @Value("${fdfs.networkTimeout:#{null}}")
  37. private Integer networkTimeout;
  38. @Value("${fdfs.connectTimeout:#{null}}")
  39. private Integer connectTimeout;
  40. @Value("${fdfs.trackerServer:#{null}}")
  41. List<String> trackerServer;
  42. @Value("${fdfs.charset:#{null}}")
  43. private String charset;
  44. @Value("${fdfs.trackerHttpPort:#{null}}")
  45. private Integer trackerHttpPort;
  46. @Value("${fdfs.antiStealToken:#{null}}")
  47. private Boolean antiStealToken;
  48. @Value("${fdfs.secretKey:#{null}}")
  49. private String secretKey;
  50. @Bean
  51. public TrackerClient getTrackerClient() throws MyException {
  52. String[] parts;
  53. ClientGlobal.setG_anti_steal_token(antiStealToken);
  54. ClientGlobal.setG_charset(charset);
  55. ClientGlobal.setG_connect_timeout(connectTimeout*1000);
  56. ClientGlobal.setG_network_timeout(networkTimeout*1000);
  57. ClientGlobal.setG_secret_key(secretKey);
  58. InetSocketAddress[] tracker_servers = new InetSocketAddress[trackerServer.size()];
  59. for (int i=0; i<trackerServer.size(); i++)
  60. {
  61. parts =trackerServer.get(i).split("\\:", 2);
  62. if (parts.length != 2)
  63. {
  64. throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
  65. }
  66. tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
  67. }
  68. ClientGlobal.setG_tracker_group(new TrackerGroup(tracker_servers));
  69. ClientGlobal.setG_tracker_http_port(trackerHttpPort);
  70. TrackerClient trackerClient = new TrackerClient();
  71. return trackerClient;
  72. }
  73. }