FdfsUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.fdkankan.common.util;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.lang.UUID;
  4. import cn.hutool.http.HttpRequest;
  5. import cn.hutool.http.HttpResponse;
  6. import com.alibaba.fastjson.JSON;
  7. import com.fdkankan.scene.bean.TietaResBean;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import java.io.File;
  12. import java.util.Date;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. @Component
  16. @Slf4j
  17. public class FdfsUtil {
  18. @Value("${fdfs.address}")
  19. private String address;
  20. @Value("${fdfs.api.getSignature}")
  21. private String api_getSignature;
  22. @Value("${fdfs.api.uploadFile}")
  23. private String api_uploadFile;
  24. @Value("${fdfs.sysCode}")
  25. private String sysCode;
  26. public String getSignature(String nonce, String timestamp){
  27. Map<String, String> headers = new HashMap<>();
  28. headers.put("timestamp", timestamp);
  29. headers.put("nonce", nonce);
  30. headers.put("sysCode", sysCode);
  31. HttpRequest httpRequest = HttpRequest.post(address.concat(api_getSignature)).addHeaders(headers).timeout(5000);
  32. HttpResponse res = httpRequest.execute();
  33. String resBody = res.body();
  34. res.close();
  35. TietaResBean<String> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
  36. String code = tietaResBean.getCode();
  37. if(!"000000".equals(code)){
  38. throw new RuntimeException("获取签名失败, code:" + code);
  39. }
  40. return tietaResBean.getData();
  41. }
  42. public Map<String, String> uploadFile(String nonce, String timestamp, String signature, String filePath){
  43. Map<String, String> headers = new HashMap<>();
  44. headers.put("timestamp", timestamp);
  45. headers.put("nonce", nonce);
  46. headers.put("sysCode", sysCode);
  47. headers.put("signature", signature);
  48. Map<String, Object> test = new HashMap<>();
  49. test.put("visibilityLevel", "1003");
  50. test.put("file", new File(filePath));
  51. test.put("userId", "111111");
  52. HttpRequest httpRequest = HttpRequest.post(address.concat(api_uploadFile)).form(test).addHeaders(headers).timeout(120000);
  53. HttpResponse res = httpRequest.execute();
  54. String resBody = res.body();
  55. log.info("upload file response : {}", resBody);
  56. res.close();
  57. TietaResBean<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
  58. String code = tietaResBean.getCode();
  59. if(!"000000".equals(code)){
  60. throw new RuntimeException("上传文件失败, code:" + code);
  61. }
  62. return tietaResBean.getData();
  63. }
  64. public Map<String, String> uploadFile(String filePath){
  65. String nonce = UUID.fastUUID().toString();
  66. String timestamp = String.valueOf(new Date().getTime());
  67. String signature = getSignature(nonce, timestamp);
  68. Map<String, String> stringStringMap = null;
  69. for(int i = 0; i< 5; i++){
  70. try {
  71. stringStringMap = uploadFile(nonce, timestamp, signature, filePath);
  72. if(CollUtil.isNotEmpty(stringStringMap)){
  73. break;
  74. }
  75. }catch (Exception e){
  76. log.warn("第{}上传文件失败,path:{}", i + 1, filePath, e);
  77. }
  78. }
  79. if(CollUtil.isEmpty(stringStringMap)){
  80. throw new RuntimeException("上传文件FASTDFS失败,path:{}" + filePath);
  81. }
  82. return stringStringMap;
  83. }
  84. public static void main(String[] args) {
  85. Map<String, String> headers = new HashMap<>();
  86. headers.put("timestamp", "1719389524320");
  87. headers.put("nonce", "123123");
  88. headers.put("sysCode", "CT00017");
  89. headers.put("signature", "3044022062501c9896a919d81d00216379a84c7d89b2d7315a22f89aee2ce7c1185f656c02206d4694fb685247a289e1c0d11e7492311ef66354c64cd2234fa593e02a635074");
  90. Map<String, Object> test = new HashMap<>();
  91. test.put("visibilityLevel", "1003");
  92. test.put("file", new File("D:\\四维时代\\中国铁塔\\数据推送示例\\KK-R4YYV4yIZlT\\images\\4k\\ff54999789b970e64e2845a2337954f9.jpg"));
  93. test.put("userId", "111111");
  94. HttpRequest httpRequest = HttpRequest.post("http://10.180.22.41:8761/ChinatowerFileService/uploadFile/").form(test).addHeaders(headers).timeout(60000);
  95. HttpResponse res = httpRequest.execute();
  96. String resBody = res.body();
  97. res.close();
  98. TietaResBean<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
  99. String code = tietaResBean.getCode();
  100. if(!"000000".equals(code)){
  101. throw new RuntimeException("上传文件失败, code:" + code);
  102. }
  103. Map<String, String> data = tietaResBean.getData();
  104. System.out.println(data);
  105. }
  106. }