|
|
|
|
|
|
1
|
package com.ruoyi.common.utils.file;
|
1
|
package com.ruoyi.common.utils.file;
|
|
2
|
|
2
|
|
|
|
|
3
|
+import cn.hutool.json.JSONObject;
|
|
3
|
import com.ruoyi.common.config.EsignDemoConfig;
|
4
|
import com.ruoyi.common.config.EsignDemoConfig;
|
|
4
|
import com.ruoyi.common.constant.EsignHeaderConstant;
|
5
|
import com.ruoyi.common.constant.EsignHeaderConstant;
|
|
5
|
import com.ruoyi.common.core.domain.entity.EsignHttpResponse;
|
6
|
import com.ruoyi.common.core.domain.entity.EsignHttpResponse;
|
|
|
|
|
|
|
16
|
private static String eSignAppSecret=EsignDemoConfig.EsignAppSecret;
|
17
|
private static String eSignAppSecret=EsignDemoConfig.EsignAppSecret;
|
|
17
|
/**
|
18
|
/**
|
|
18
|
* 获取文件上传地址
|
19
|
* 获取文件上传地址
|
|
19
|
- *
|
|
|
|
20
|
- * @return
|
|
|
|
21
|
*/
|
20
|
*/
|
|
22
|
public static EsignHttpResponse getUploadUrl(String filePath) throws EsignDemoException {
|
21
|
public static EsignHttpResponse getUploadUrl(String filePath) throws EsignDemoException {
|
|
23
|
//自定义的文件封装类,传入文件地址可以获取文件的名称大小,文件流等数据
|
22
|
//自定义的文件封装类,传入文件地址可以获取文件的名称大小,文件流等数据
|
|
|
|
|
|
|
38
|
//发起接口请求
|
37
|
//发起接口请求
|
|
39
|
return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
|
38
|
return EsignHttpHelper.doCommHttp(eSignHost, apiaddr, requestType, jsonParm, header, true);
|
|
40
|
}
|
39
|
}
|
|
|
|
40
|
+ /**
|
|
|
|
41
|
+ * 上传文件流
|
|
|
|
42
|
+ */
|
|
|
|
43
|
+ public static EsignHttpResponse uploadFile(String uploadUrl,String filePath) throws EsignDemoException {
|
|
|
|
44
|
+ //根据文件地址获取文件contentMd5
|
|
|
|
45
|
+ EsignFileBean esignFileBean = new EsignFileBean(filePath);
|
|
|
|
46
|
+ //请求方法
|
|
|
|
47
|
+ EsignRequestType requestType= EsignRequestType.PUT;
|
|
|
|
48
|
+ return EsignHttpHelper.doUploadHttp(uploadUrl,requestType,esignFileBean.getFileBytes(),esignFileBean.getFileContentMD5(), EsignHeaderConstant.CONTENTTYPE_STREAM.VALUE(),true);
|
|
|
|
49
|
+ }
|
|
41
|
|
50
|
|
|
|
|
51
|
+ /**
|
|
|
|
52
|
+ * 获取文件上传状态
|
|
|
|
53
|
+ */
|
|
|
|
54
|
+ public static EsignHttpResponse getFileStatus(String fileId) throws EsignDemoException {
|
|
|
|
55
|
+ String apiaddr="/v3/files/"+fileId;
|
|
|
|
56
|
+
|
|
|
|
57
|
+ //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
|
|
|
|
58
|
+ String jsonParm=null;
|
|
|
|
59
|
+ //请求方法
|
|
|
|
60
|
+ EsignRequestType requestType= EsignRequestType.GET;
|
|
|
|
61
|
+ //生成签名鉴权方式的的header
|
|
|
|
62
|
+ Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
|
|
|
|
63
|
+ //发起接口请求
|
|
|
|
64
|
+ return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
|
|
|
|
65
|
+ }
|
|
42
|
|
66
|
|
|
|
|
67
|
+/* public static void main(String[] args) throws EsignDemoException {
|
|
|
|
68
|
+ String filePath = "D:\\home\\ruoyi\\uploadPath\\upload\\2023\\10\\07\\6babb8e261454fffbacf6d9fd1589b9b.docx";
|
|
|
|
69
|
+ EsignHttpResponse uploadUrl = getUploadUrl(filePath);
|
|
|
|
70
|
+ String body = uploadUrl.getBody();
|
|
|
|
71
|
+ JSONObject jsonObject = new JSONObject(body);
|
|
|
|
72
|
+ JSONObject dataObj = jsonObject.getJSONObject("data");
|
|
|
|
73
|
+ String fileUploadUrl = dataObj.get("fileUploadUrl").toString();
|
|
|
|
74
|
+ System.out.println("这是fileUploadUrl:"+fileUploadUrl);
|
|
|
|
75
|
+ String fileId = dataObj.get("fileId").toString();
|
|
|
|
76
|
+ System.out.println("这是fileId:"+fileId);
|
|
|
|
77
|
+ EsignHttpResponse esignHttpResponse = uploadFile(fileUploadUrl, filePath);
|
|
|
|
78
|
+ System.out.println("这是上传文件流的结果:"+esignHttpResponse.getBody());
|
|
|
|
79
|
+ EsignHttpResponse fileStatus = getFileStatus(fileId);
|
|
|
|
80
|
+ System.out.println("这是获取文件上传状态的结果:"+fileStatus.getBody());
|
|
|
|
81
|
+ getFileStatus("95d0c307d91e4985bdb8874f6f84daa5");
|
|
|
|
82
|
+ }*/
|
|
43
|
}
|
83
|
}
|