123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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<String, String> 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<String> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
- String code = tietaResBean.getCode();
- if(!"000000".equals(code)){
- throw new RuntimeException("获取签名失败, code:" + code);
- }
- return tietaResBean.getData();
- }
- public Map<String, String> uploadFile(String nonce, String timestamp, String signature, String filePath){
- Map<String, String> headers = new HashMap<>();
- headers.put("timestamp", timestamp);
- headers.put("nonce", nonce);
- headers.put("sysCode", sysCode);
- headers.put("signature", signature);
- Map<String, Object> 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<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
- String code = tietaResBean.getCode();
- if(!"000000".equals(code)){
- throw new RuntimeException("上传文件失败, code:" + code);
- }
- return tietaResBean.getData();
- }
- public Map<String, String> uploadFile(String filePath){
- String nonce = UUID.fastUUID().toString();
- String timestamp = String.valueOf(new Date().getTime());
- String signature = getSignature(nonce, timestamp);
- Map<String, String> 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<String, String> headers = new HashMap<>();
- headers.put("timestamp", "1719389524320");
- headers.put("nonce", "123123");
- headers.put("sysCode", "CT00017");
- headers.put("signature", "3044022062501c9896a919d81d00216379a84c7d89b2d7315a22f89aee2ce7c1185f656c02206d4694fb685247a289e1c0d11e7492311ef66354c64cd2234fa593e02a635074");
- Map<String, Object> 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<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
- String code = tietaResBean.getCode();
- if(!"000000".equals(code)){
- throw new RuntimeException("上传文件失败, code:" + code);
- }
- Map<String, String> data = tietaResBean.getData();
- System.out.println(data);
- }
- }
|