|
|
@@ -1,135 +1,135 @@
|
|
|
-package com.fdkankan.modeldemo.utils;
|
|
|
-
|
|
|
-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.modeldemo.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 {
|
|
|
-
|
|
|
- private final static String TIMESTAMP_KEY = "timestamp";
|
|
|
-
|
|
|
- private final static String NONCE_KEY = "nonce";
|
|
|
-
|
|
|
- private final static String SYS_CODE_KEY = "sysCode";
|
|
|
-
|
|
|
- private final static String FDFS_SUCCESS_CODE = "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;
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取签名
|
|
|
- * @param nonce
|
|
|
- * @param timestamp
|
|
|
- * @return
|
|
|
- */
|
|
|
- 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(SYS_CODE_KEY, sysCode);
|
|
|
- //发送请求
|
|
|
- HttpRequest httpRequest = HttpRequest.post(address.concat(api_getSignature)).addHeaders(headers).timeout(5000);
|
|
|
- HttpResponse res = httpRequest.execute();
|
|
|
- String resBody = res.body();
|
|
|
- //关闭流
|
|
|
- res.close();
|
|
|
- //请求结果转换为bean对象
|
|
|
- TietaResBean<String> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
|
|
|
- String code = tietaResBean.getCode();
|
|
|
- //状态吗错误,抛出异常
|
|
|
- if(!FDFS_SUCCESS_CODE.equals(code)){
|
|
|
- throw new RuntimeException("获取签名失败, code:" + code);
|
|
|
- }
|
|
|
- return tietaResBean.getData();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传文件
|
|
|
- * @param nonce
|
|
|
- * @param timestamp
|
|
|
- * @param signature
|
|
|
- * @param filePath
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Map<String, String> uploadFile(String nonce, String timestamp, String signature, String filePath){
|
|
|
-
|
|
|
- Map<String, String> headers = new HashMap<>();
|
|
|
- headers.put(TIMESTAMP_KEY, timestamp);
|
|
|
- headers.put(NONCE_KEY, nonce);
|
|
|
- headers.put(SYS_CODE_KEY, 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();
|
|
|
- //str转对象
|
|
|
- TietaResBean<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
|
|
|
- String code = tietaResBean.getCode();
|
|
|
- if(!FDFS_SUCCESS_CODE.equals(code)){
|
|
|
- //上传失败,抛出异常
|
|
|
- throw new RuntimeException("上传文件失败, code:" + code);
|
|
|
- }
|
|
|
- return tietaResBean.getData();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传文件,其中包括获取签名以及上传文件两步
|
|
|
- * @param filePath
|
|
|
- * @return
|
|
|
- */
|
|
|
- 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;
|
|
|
- //上传文件,失败后重试4次
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
+//package com.fdkankan.modeldemo.utils;
|
|
|
+//
|
|
|
+//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.modeldemo.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 {
|
|
|
+//
|
|
|
+// private final static String TIMESTAMP_KEY = "timestamp";
|
|
|
+//
|
|
|
+// private final static String NONCE_KEY = "nonce";
|
|
|
+//
|
|
|
+// private final static String SYS_CODE_KEY = "sysCode";
|
|
|
+//
|
|
|
+// private final static String FDFS_SUCCESS_CODE = "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;
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 获取签名
|
|
|
+// * @param nonce
|
|
|
+// * @param timestamp
|
|
|
+// * @return
|
|
|
+// */
|
|
|
+// 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(SYS_CODE_KEY, sysCode);
|
|
|
+// //发送请求
|
|
|
+// HttpRequest httpRequest = HttpRequest.post(address.concat(api_getSignature)).addHeaders(headers).timeout(5000);
|
|
|
+// HttpResponse res = httpRequest.execute();
|
|
|
+// String resBody = res.body();
|
|
|
+// //关闭流
|
|
|
+// res.close();
|
|
|
+// //请求结果转换为bean对象
|
|
|
+// TietaResBean<String> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
|
|
|
+// String code = tietaResBean.getCode();
|
|
|
+// //状态吗错误,抛出异常
|
|
|
+// if(!FDFS_SUCCESS_CODE.equals(code)){
|
|
|
+// throw new RuntimeException("获取签名失败, code:" + code);
|
|
|
+// }
|
|
|
+// return tietaResBean.getData();
|
|
|
+// }
|
|
|
+//
|
|
|
+//// /**
|
|
|
+//// * 上传文件
|
|
|
+//// * @param nonce
|
|
|
+//// * @param timestamp
|
|
|
+//// * @param signature
|
|
|
+//// * @param filePath
|
|
|
+//// * @return
|
|
|
+//// */
|
|
|
+//// public Map<String, String> uploadFile(String nonce, String timestamp, String signature, String filePath){
|
|
|
+////
|
|
|
+//// Map<String, String> headers = new HashMap<>();
|
|
|
+//// headers.put(TIMESTAMP_KEY, timestamp);
|
|
|
+//// headers.put(NONCE_KEY, nonce);
|
|
|
+//// headers.put(SYS_CODE_KEY, 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();
|
|
|
+//// //str转对象
|
|
|
+//// TietaResBean<Map<String, String>> tietaResBean = JSON.parseObject(resBody, TietaResBean.class);
|
|
|
+//// String code = tietaResBean.getCode();
|
|
|
+//// if(!FDFS_SUCCESS_CODE.equals(code)){
|
|
|
+//// //上传失败,抛出异常
|
|
|
+//// throw new RuntimeException("上传文件失败, code:" + code);
|
|
|
+//// }
|
|
|
+//// return tietaResBean.getData();
|
|
|
+//// }
|
|
|
+//
|
|
|
+//// /**
|
|
|
+//// * 上传文件,其中包括获取签名以及上传文件两步
|
|
|
+//// * @param filePath
|
|
|
+//// * @return
|
|
|
+//// */
|
|
|
+//// 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;
|
|
|
+//// //上传文件,失败后重试4次
|
|
|
+//// 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;
|
|
|
+//// }
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//}
|