123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 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<String, String> 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<String> 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<String, String> uploadFile(String nonce, String timestamp, String signature, String filePath) throws ValidationException {
- Map<String, String> headers = new HashMap<>();
- headers.put(timestamp_key, timestamp);
- headers.put(nonce_key, nonce);
- headers.put(sysCode_key, sysCode);
- headers.put("signature", signature);
- Map<String, Object> 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<Map<String, String>> 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<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) {
- String timestamp = String.valueOf(new Date().getTime());
- Map<String, String> headers = new HashMap<>();
- headers.put(timestamp_key, timestamp);
- headers.put(nonce_key, "123123");
- headers.put(sysCode_key, "CT00017");
- headers.put("signature", "3044022062501c9896a919d81d00216379a84c7d89b2d7315a22f89aee2ce7c1185f656c02206d4694fb685247a289e1c0d11e7492311ef66354c64cd2234fa593e02a635074");
- Map<String, Object> 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<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
- String code = tietaResBean.getCode();
- if(!success_status.equals(code)){
- throw new RuntimeException("上传文件失败, code:" + code);
- }
- Map<String, String> data = tietaResBean.getData();
- System.out.println(data);
- }
- }
|