package com.fdkankan.common.util; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; 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.owasp.esapi.errors.ValidationException; 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 { private final static String timestamp_key = "timestamp"; private final static String nonce_key = "nonce"; private final static String sysCode_key = "sysCode"; private final static String success_status = "000000"; @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_key, timestamp); headers.put(nonce_key, nonce); headers.put(sysCode_key, 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(!success_status.equals(code)){ throw new RuntimeException("获取签名失败, code:" + code); } return tietaResBean.getData(); } public Map uploadFile(String nonce, String timestamp, String signature, String filePath) throws ValidationException { Map headers = new HashMap<>(); headers.put(timestamp_key, timestamp); headers.put(nonce_key, nonce); headers.put(sysCode_key, sysCode); headers.put("signature", signature); Map test = new HashMap<>(); test.put("visibilityLevel", "1003"); String parentPath = FileUtil.getParent(filePath, 1); ESAPIUtil.validFilePath(parentPath); 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(!success_status.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) { String timestamp = String.valueOf(new Date().getTime()); Map headers = new HashMap<>(); headers.put(timestamp_key, timestamp); headers.put(nonce_key, "123123"); headers.put(sysCode_key, "CT00017"); headers.put("signature", "3044022062501c9896a919d81d00216379a84c7d89b2d7315a22f89aee2ce7c1185f656c02206d4694fb685247a289e1c0d11e7492311ef66354c64cd2234fa593e02a635074"); Map test = new HashMap<>(); test.put("visibilityLevel", "1003"); test.put("file", new File("C:\\Users\\dsx\\Downloads\\vision.txt")); 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(!success_status.equals(code)){ throw new RuntimeException("上传文件失败, code:" + code); } Map data = tietaResBean.getData(); System.out.println(data); } }