获取文件上传地址
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
|
||||
import com.ruoyi.common.constant.EsignEncryption;
|
||||
import com.ruoyi.common.constant.EsignHeaderConstant;
|
||||
import com.ruoyi.common.constant.EsignHttpCfgHelper;
|
||||
import com.ruoyi.common.core.domain.entity.EsignCoreSdkInfo;
|
||||
import com.ruoyi.common.core.domain.entity.EsignHttpResponse;
|
||||
import com.ruoyi.common.enums.EsignRequestType;
|
||||
import com.ruoyi.common.exception.EsignDemoException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description Http 请求 辅助类
|
||||
* @author 澄泓
|
||||
* @since JDK1.7
|
||||
*/
|
||||
public class EsignHttpHelper {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EsignHttpHelper.class);
|
||||
|
||||
/**
|
||||
* 不允许外部创建实例
|
||||
*/
|
||||
private EsignHttpHelper() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 发送常规HTTP 请求
|
||||
*
|
||||
* @param reqType 请求方式
|
||||
* @param url 请求路径
|
||||
* @param paramStr 请求参数
|
||||
* @return
|
||||
* @throws EsignDemoException
|
||||
* @author 澄泓
|
||||
*/
|
||||
public static EsignHttpResponse doCommHttp(String host, String url, EsignRequestType reqType, Object paramStr , Map<String,String> httpHeader, boolean debug) throws EsignDemoException {
|
||||
|
||||
return EsignHttpCfgHelper.sendHttp(reqType, host+url,httpHeader, paramStr, debug);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description 发送文件流上传 HTTP 请求
|
||||
*
|
||||
* @param reqType 请求方式
|
||||
* @param uploadUrl 请求路径
|
||||
* @param param 请求参数
|
||||
* @param fileContentMd5 文件fileContentMd5
|
||||
* @param contentType 文件MIME类型
|
||||
* @return
|
||||
* @throws EsignDemoException
|
||||
* @author 澄泓
|
||||
*/
|
||||
public static EsignHttpResponse doUploadHttp( String uploadUrl,EsignRequestType reqType,byte[] param, String fileContentMd5,
|
||||
String contentType, boolean debug) throws EsignDemoException {
|
||||
Map<String, String> uploadHeader = buildUploadHeader(fileContentMd5, contentType);
|
||||
if(debug){
|
||||
LOGGER.info("----------------------------start------------------------");
|
||||
LOGGER.info("fileContentMd5:{}",fileContentMd5);
|
||||
LOGGER.info("contentType:{}",contentType);
|
||||
}
|
||||
return EsignHttpCfgHelper.sendHttp(reqType,uploadUrl, uploadHeader, param,debug);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @description 构建一个签名鉴权+json数据的esign请求头
|
||||
* @return
|
||||
* @author 澄泓
|
||||
*/
|
||||
public static Map<String, String> buildSignAndJsonHeader(String projectId,String contentMD5,String accept,String contentType,String authMode) {
|
||||
|
||||
Map<String, String> header = new HashMap<>();
|
||||
header.put("X-Tsign-Open-App-Id", projectId);
|
||||
header.put("X-Tsign-Open-Version-Sdk",EsignCoreSdkInfo.getSdkVersion());
|
||||
header.put("X-Tsign-Open-Ca-Timestamp", EsignEncryption.timeStamp());
|
||||
header.put("Accept",accept);
|
||||
header.put("Content-MD5",contentMD5);
|
||||
header.put("Content-Type", contentType);
|
||||
header.put("X-Tsign-Open-Auth-Mode", authMode);
|
||||
return header;
|
||||
}
|
||||
|
||||
/**
|
||||
* 签名计算并且构建一个签名鉴权+json数据的esign请求头
|
||||
* @param httpMethod
|
||||
* * The name of a supported {@linkplain java.nio.charset.Charset
|
||||
* * charset}
|
||||
* @return
|
||||
*/
|
||||
public static Map<String,String> signAndBuildSignAndJsonHeader(String projectId, String secret,String paramStr,String httpMethod,String url,boolean debug) throws EsignDemoException {
|
||||
String contentMD5="";
|
||||
//统一转大写处理
|
||||
httpMethod = httpMethod.toUpperCase();
|
||||
if("GET".equals(httpMethod)||"DELETE".equals(httpMethod)){
|
||||
paramStr=null;
|
||||
contentMD5="";
|
||||
} else if("PUT".equals(httpMethod)||"POST".equals(httpMethod)){
|
||||
//对body体做md5摘要
|
||||
contentMD5= EsignEncryption.doContentMD5(paramStr);
|
||||
}else{
|
||||
throw new EsignDemoException(String.format("不支持的请求方法%s",httpMethod));
|
||||
}
|
||||
//构造一个初步的请求头
|
||||
Map<String, String> esignHeaderMap = buildSignAndJsonHeader(projectId, contentMD5, EsignHeaderConstant.ACCEPT.VALUE(), EsignHeaderConstant.CONTENTTYPE_JSON.VALUE(), EsignHeaderConstant.AUTHMODE.VALUE());
|
||||
//排序
|
||||
url=EsignEncryption.sortApiUrl(url);
|
||||
//传入生成的bodyMd5,加上其他请求头部信息拼接成字符串
|
||||
String message = EsignEncryption.appendSignDataString(httpMethod, esignHeaderMap.get("Content-MD5"),esignHeaderMap.get("Accept"),esignHeaderMap.get("Content-Type"),esignHeaderMap.get("Headers"),esignHeaderMap.get("Date"), url);
|
||||
//整体做sha256签名
|
||||
String reqSignature = EsignEncryption.doSignatureBase64(message, secret);
|
||||
//请求头添加签名值
|
||||
esignHeaderMap.put("X-Tsign-Open-Ca-Signature",reqSignature);
|
||||
if(debug){
|
||||
LOGGER.info("----------------------------start------------------------");
|
||||
LOGGER.info("待计算body值:{}", paramStr+"\n");
|
||||
LOGGER.info("MD5值:{}",contentMD5+"\n");
|
||||
LOGGER.info("待签名字符串:{}",message+"\n");
|
||||
LOGGER.info("签名值:{}",reqSignature+"\n");
|
||||
}
|
||||
return esignHeaderMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description 构建一个Token鉴权+jsons数据的esign请求头
|
||||
* @return
|
||||
* @author 澄泓
|
||||
*/
|
||||
public static Map<String, String> buildTokenAndJsonHeader(String appid,String token) {
|
||||
Map<String, String> esignHeader = new HashMap<>();
|
||||
esignHeader.put("X-Tsign-Open-Version-Sdk", EsignCoreSdkInfo.getSdkVersion());
|
||||
esignHeader.put("Content-Type", EsignHeaderConstant.CONTENTTYPE_JSON.VALUE());
|
||||
esignHeader.put("X-Tsign-Open-App-Id", appid);
|
||||
esignHeader.put("X-Tsign-Open-Token", token);
|
||||
return esignHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 构建一个form表单数据的esign请求头
|
||||
* @return
|
||||
* @author 澄泓
|
||||
*/
|
||||
public static Map<String, String> buildFormDataHeader(String appid) {
|
||||
Map<String, String> esignHeader = new HashMap<>();
|
||||
esignHeader.put("X-Tsign-Open-Version-Sdk",EsignCoreSdkInfo.getSdkVersion());
|
||||
esignHeader.put("X-Tsign-Open-Authorization-Version","v2");
|
||||
esignHeader.put("Content-Type", EsignHeaderConstant.CONTENTTYPE_FORMDATA.VALUE());
|
||||
esignHeader.put("X-Tsign-Open-App-Id", appid);
|
||||
return esignHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 创建文件流上传 请求头
|
||||
*
|
||||
* @param fileContentMd5
|
||||
* @param contentType
|
||||
* @return
|
||||
* @author 澄泓
|
||||
*/
|
||||
public static Map<String, String> buildUploadHeader(String fileContentMd5, String contentType) {
|
||||
Map<String, String> header = new HashMap<>();
|
||||
header.put("Content-MD5", fileContentMd5);
|
||||
header.put("Content-Type", contentType);
|
||||
|
||||
return header;
|
||||
}
|
||||
|
||||
// ------------------------------私有方法end----------------------------------------------
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.ruoyi.common.utils.bean;
|
||||
|
||||
|
||||
import com.ruoyi.common.constant.FileTransformation;
|
||||
import com.ruoyi.common.exception.EsignDemoException;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @description 文件基础信息封装类
|
||||
* @author 澄泓
|
||||
* @date 2020/10/26 14:54
|
||||
* @version JDK1.7
|
||||
*/
|
||||
public class EsignFileBean {
|
||||
//文件名称
|
||||
private String fileName;
|
||||
//文件大小
|
||||
private int fileSize;
|
||||
//文件内容MD5
|
||||
private String fileContentMD5;
|
||||
//文件地址
|
||||
private String filePath;
|
||||
|
||||
|
||||
public EsignFileBean(String filePath) throws EsignDemoException {
|
||||
this.filePath=filePath;
|
||||
this.fileContentMD5 = FileTransformation.getFileContentMD5(filePath);
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
throw new EsignDemoException("文件不存在");
|
||||
}
|
||||
this.fileName = file.getName();
|
||||
this.fileSize = (int) file.length();
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public int getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public String getFileContentMD5() {
|
||||
return fileContentMD5;
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入本地文件地址获取二进制数据
|
||||
* @return
|
||||
* @throws EsignDemoException
|
||||
*/
|
||||
public byte[] getFileBytes() throws EsignDemoException {
|
||||
return FileTransformation.fileToBytes(filePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.ruoyi.common.utils.file;
|
||||
|
||||
import com.ruoyi.common.config.EsignDemoConfig;
|
||||
import com.ruoyi.common.constant.EsignHeaderConstant;
|
||||
import com.ruoyi.common.core.domain.entity.EsignHttpResponse;
|
||||
import com.ruoyi.common.enums.EsignRequestType;
|
||||
import com.ruoyi.common.exception.EsignDemoException;
|
||||
import com.ruoyi.common.utils.EsignHttpHelper;
|
||||
import com.ruoyi.common.utils.bean.EsignFileBean;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class SaaSAPIFileUtils {
|
||||
private static String eSignHost= EsignDemoConfig.EsignHost;
|
||||
private static String eSignAppId= EsignDemoConfig.EsignAppId;
|
||||
private static String eSignAppSecret=EsignDemoConfig.EsignAppSecret;
|
||||
/**
|
||||
* 获取文件上传地址
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static EsignHttpResponse getUploadUrl(String filePath) throws EsignDemoException {
|
||||
//自定义的文件封装类,传入文件地址可以获取文件的名称大小,文件流等数据
|
||||
EsignFileBean esignFileBean = new EsignFileBean(filePath);
|
||||
String apiaddr = "/v3/files/file-upload-url";
|
||||
//请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
|
||||
String jsonParm = "{\n" +
|
||||
" \"contentMd5\": \"" + esignFileBean.getFileContentMD5() + "\",\n" +
|
||||
" \"fileName\":\"" + esignFileBean.getFileName() + "\"," +
|
||||
" \"fileSize\": " + esignFileBean.getFileSize() + ",\n" +
|
||||
" \"convertToPDF\":" +true+ ",\n" +
|
||||
" \"contentType\": \"" + EsignHeaderConstant.CONTENTTYPE_STREAM.VALUE() + "\"\n" +
|
||||
"}";
|
||||
//请求方法
|
||||
EsignRequestType requestType = EsignRequestType.POST;
|
||||
//生成签名鉴权方式的的header
|
||||
Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId, eSignAppSecret, jsonParm, requestType.name(), apiaddr, true);
|
||||
//发起接口请求
|
||||
return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user