package com.fdkankan.common.util; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.UUID; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.fdkankan.scene.bean.TietaResBean; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.File; import java.util.Date; import java.util.HashMap; import java.util.Map; @Component @Slf4j public class FdfsUtil { @Value("${fdfs.address}") private String address; @Value("${fdfs.api.getSignature}") private String api_getSignature; @Value("${fdfs.api.uploadFile}") private String api_uploadFile; @Value("${fdfs.sysCode}") private String sysCode; public String getSignature(String nonce, String timestamp){ Map headers = new HashMap<>(); headers.put("timestamp", timestamp); headers.put("nonce", nonce); headers.put("sysCode", sysCode); HttpRequest httpRequest = HttpRequest.post(address.concat(api_getSignature)).addHeaders(headers).timeout(5000); HttpResponse res = httpRequest.execute(); String resBody = res.body(); res.close(); TietaResBean tietaResBean = JSON.parseObject(resBody, TietaResBean.class); String code = tietaResBean.getCode(); if(!"000000".equals(code)){ throw new RuntimeException("获取签名失败, code:" + code); } return tietaResBean.getData(); } public Map uploadFile(String nonce, String timestamp, String signature, String filePath){ Map headers = new HashMap<>(); headers.put("timestamp", timestamp); headers.put("nonce", nonce); headers.put("sysCode", sysCode); headers.put("signature", signature); Map test = new HashMap<>(); test.put("visibilityLevel", "1003"); test.put("file", new File(filePath)); test.put("userId", "111111"); HttpRequest httpRequest = HttpRequest.post(address.concat(api_uploadFile)).form(test).addHeaders(headers).timeout(120000); HttpResponse res = httpRequest.execute(); String resBody = res.body(); log.info("upload file response : {}", resBody); res.close(); TietaResBean> tietaResBean = JSON.parseObject(resBody, TietaResBean.class); String code = tietaResBean.getCode(); if(!"000000".equals(code)){ throw new RuntimeException("上传文件失败, code:" + code); } return tietaResBean.getData(); } public Map uploadFile(String filePath){ String nonce = UUID.fastUUID().toString(); String timestamp = String.valueOf(new Date().getTime()); String signature = getSignature(nonce, timestamp); Map stringStringMap = null; for(int i = 0; i< 5; i++){ try { stringStringMap = uploadFile(nonce, timestamp, signature, filePath); if(CollUtil.isNotEmpty(stringStringMap)){ break; } }catch (Exception e){ log.warn("第{}上传文件失败,path:{}", i + 1, filePath, e); } } if(CollUtil.isEmpty(stringStringMap)){ throw new RuntimeException("上传文件FASTDFS失败,path:{}" + filePath); } return stringStringMap; } public static void main(String[] args) { Map headers = new HashMap<>(); headers.put("timestamp", "1719389524320"); headers.put("nonce", "123123"); headers.put("sysCode", "CT00017"); headers.put("signature", "3044022062501c9896a919d81d00216379a84c7d89b2d7315a22f89aee2ce7c1185f656c02206d4694fb685247a289e1c0d11e7492311ef66354c64cd2234fa593e02a635074"); Map test = new HashMap<>(); test.put("visibilityLevel", "1003"); test.put("file", new File("D:\\四维时代\\中国铁塔\\数据推送示例\\KK-R4YYV4yIZlT\\images\\4k\\ff54999789b970e64e2845a2337954f9.jpg")); test.put("userId", "111111"); HttpRequest httpRequest = HttpRequest.post("http://10.180.22.41:8761/ChinatowerFileService/uploadFile/").form(test).addHeaders(headers).timeout(60000); HttpResponse res = httpRequest.execute(); String resBody = res.body(); res.close(); TietaResBean> tietaResBean = JSON.parseObject(resBody, TietaResBean.class); String code = tietaResBean.getCode(); if(!"000000".equals(code)){ throw new RuntimeException("上传文件失败, code:" + code); } Map data = tietaResBean.getData(); System.out.println(data); } }