FdfsUtil.java 5.3 KB

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