com.hn.dingtalk.DingSpaceUtil Maven / Gradle / Ivy
package com.hn.dingtalk;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.hn.dingtalk.constant.DingConstant;
import com.hn.dingtalk.exception.DingTalkException;
import com.hn.dingtalk.domain.DingSpaceFile;
import com.taobao.api.ApiException;
import com.taobao.api.FileItem;
import com.taobao.api.internal.util.WebUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
* 描述:
* 钉盘管理
*
* @author fei
* 2019-07-27 15:49
*/
@Slf4j
public class DingSpaceUtil {
public static DingSpaceFile saveFile(String code, String userId,
String filePath, String fileName){
System.out.println("---------------------------");
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/cspace/add");
OapiCspaceAddRequest request = new OapiCspaceAddRequest();
request.setAgentId(DingConstant.AGENTID+"");
request.setCode(code);
request.setMediaId(uploadMedia(filePath));
// request.setMediaId(uploadTrunk());
request.setSpaceId(getSpaceId(userId)+"");
request.setFolderId("0");
request.setName(fileName);
request.setOverwrite(true);
request.setHttpMethod("GET");
try {
System.out.println("---------------------------");
OapiCspaceAddResponse response = client.execute(request,AccessTokenUtil.getToken());
log.info("保存钉盘文件成功:{}",JSONUtil.toJsonStr(response));
JSONObject resultImage = JSONUtil.parseObj(response.getDentry());
DingSpaceFile dingSpaceFile = new DingSpaceFile();
dingSpaceFile.setFileId(resultImage.getStr("id"));
dingSpaceFile.setFileSize(resultImage.getStr("size"));
dingSpaceFile.setFileType(resultImage.getStr("contentType"));
dingSpaceFile.setSpaceId(resultImage.getStr("spaceId"));
dingSpaceFile.setFileName(resultImage.getStr("name"));
return dingSpaceFile;
} catch (ApiException e) {
throw new DingTalkException("保存钉盘文件失败:"+e.getErrMsg());
}
}
public static Long getSpaceId(String userId) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/cspace/info");
OapiProcessinstanceCspaceInfoRequest req = new OapiProcessinstanceCspaceInfoRequest();
req.setUserId(userId);
OapiProcessinstanceCspaceInfoResponse rsp = null;
try {
rsp = client.execute(req, AccessTokenUtil.getToken());
return rsp.getResult().getSpaceId();
} catch (ApiException e) {
e.printStackTrace();
}
System.out.println(rsp.getBody());
throw new DingTalkException("钉钉获取钉盘ID失败");
}
public static String uploadMedia(String filePath) {
OapiFileUploadSingleRequest request = new OapiFileUploadSingleRequest();
request.setFileSize(1000L);
request.setAgentId(DingConstant.AGENTID);
DingTalkClient client = null;
try {
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/single?"+WebUtils.buildQuery(request.getTextParams(),"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
// 必须重新new一个请求
request = new OapiFileUploadSingleRequest();
request.setFile(new FileItem(filePath));
try {
OapiFileUploadSingleResponse response = client.execute(request,AccessTokenUtil.getToken());
if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
return response.getMediaId();
}
throw new DingTalkException("钉钉上传媒体文件失败:"+response.getErrmsg());
} catch (ApiException e) {
throw new DingTalkException("钉钉上传媒体文件失败:"+e.getErrMsg());
}
}
public static String uploadTransactionBegin(Long fileSize) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/transaction");
OapiFileUploadTransactionRequest request = new OapiFileUploadTransactionRequest();
request.setAgentId(DingConstant.AGENTID);
request.setFileSize(1000L);
request.setChunkNumbers(1L);
request.setHttpMethod("GET");
try {
OapiFileUploadTransactionResponse response = client.execute(request,AccessTokenUtil.getToken());
if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
log.info(response.getUploadId());
log.info(response.getMediaId());
return response.getUploadId();
}
throw new DingTalkException("钉钉开启分块上传事务失败:"+response.getErrmsg());
} catch (ApiException e) {
throw new DingTalkException("钉钉开启分块上传事务失败:"+e.getErrMsg());
}
}
public static void uploadChunk(String uploadId) {
OapiFileUploadChunkRequest request = new OapiFileUploadChunkRequest();
request.setAgentId(DingConstant.AGENTID+"");
request.setChunkSequence(1L);
request.setUploadId(uploadId);
DingTalkClient client = null;
try {
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/chunk?"+WebUtils.buildQuery(request.getTextParams(),"utf-8"));
} catch (IOException e) {
throw new DingTalkException("钉钉上传文件块失败:"+e.getMessage());
}
request = new OapiFileUploadChunkRequest();
request.setFile(new FileItem("D:\\vhr.sql"));
try {
OapiFileUploadChunkResponse response = client.execute(request,AccessTokenUtil.getToken());
if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
log.info("钉钉上传文件块成功:{}",JSONUtil.toJsonStr(response));
return;
}
throw new DingTalkException("钉钉上传文件块失败:"+response.getErrmsg());
} catch (ApiException e) {
throw new DingTalkException("钉钉上传文件块失败:"+e.getMessage());
}
}
public static String uploadTransactionEnd(String uploadId) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/transaction");
OapiFileUploadTransactionRequest request = new OapiFileUploadTransactionRequest();
request.setAgentId(DingConstant.AGENTID+"");
request.setFileSize(1000L);
request.setChunkNumbers(1L);
request.setUploadId(uploadId);
request.setHttpMethod("GET");
try {
OapiFileUploadTransactionResponse response = client.execute(request,AccessTokenUtil.getToken());
if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
// log.info("钉钉提交文件上传事务成功:{}",JSONObject.toJSONString(response));
log.info("钉钉提交文件上传事务成功:{}",response.getMediaId());
return response.getMediaId();
}
throw new DingTalkException("钉钉提交文件上传事务失败:"+response.getErrmsg());
} catch (ApiException e) {
throw new DingTalkException("钉钉提交文件上传事务失败:"+e.getMessage());
}
}
public static Long sendMediaToUser(String userId,String mediaId,String fileName){
OapiCspaceAddToSingleChatRequest request = new OapiCspaceAddToSingleChatRequest();
request.setAgentId(DingConstant.AGENTID+"");
request.setUserid(userId);
request.setMediaId(mediaId);
request.setFileName(fileName);
DingTalkClient client = null;
try {
client = new DefaultDingTalkClient("https://oapi.dingtalk.com/cspace/add_to_single_chat?"+WebUtils.buildQuery(request.getTextParams(),"utf-8"));
} catch (IOException e) {
throw new DingTalkException("发送钉盘文件给指定用户失败:"+e.getMessage());
}
try {
OapiCspaceAddToSingleChatResponse response = client.execute(request, AccessTokenUtil.getToken());
if (DingConstant.SUCCESS_CODE.equals(response.getErrcode())) {
log.info("发送钉盘文件给指定用户成功:{}",JSONUtil.toJsonStr(response));
return response.getErrcode();
}
throw new DingTalkException("发送钉盘文件给指定用户失败:"+response.getErrmsg());
} catch (ApiException e) {
throw new DingTalkException("发送钉盘文件给指定用户失败:"+e.getErrMsg());
}
}
public static void main(String[] args)throws Exception {
/* DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/cspace/get_custom_space");
OapiCspaceGetCustomSpaceRequest request = new OapiCspaceGetCustomSpaceRequest();
request.setAgentId(DingConstant.AGENTID+"");
// 1712858442
request.setDomain("测试");
request.setHttpMethod("GET");
OapiCspaceGetCustomSpaceResponse response = client.execute(request,AccessTokenUtil.getToken());
System.out.println(response.getSpaceid());
*/
/* DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/cspace/grant_custom_space");
OapiCspaceGrantCustomSpaceRequest request = new OapiCspaceGrantCustomSpaceRequest();
request.setAgentId(DingConstant.AGENTID+"");
request.setDomain("test");
request.setType("add");
request.setUserid("0502101558117240531");
request.setPath("/test/");
request.setDuration(10000L);
request.setHttpMethod("GET");
OapiCspaceGrantCustomSpaceResponse response = client.execute(request,AccessTokenUtil.getToken());
System.out.println(response.getErrcode());
*/
/* DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/cspace/info");
OapiProcessinstanceCspaceInfoRequest req = new OapiProcessinstanceCspaceInfoRequest();
req.setUserId("0502101558117240531");
OapiProcessinstanceCspaceInfoResponse rsp = client.execute(req, AccessTokenUtil.getToken());
System.out.println(rsp.getBody());*/
/* OapiFileUploadSingleRequest request = new OapiFileUploadSingleRequest();
request.setFileSize(1000L);
request.setAgentId(DingConstant.AGENTID+"");
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/file/upload/single?"+WebUtils.buildQuery(request.getTextParams(),"utf-8"));
// 必须重新new一个请求
request = new OapiFileUploadSingleRequest();
request.setFile(new FileItem("E:\\processimg.png"));
OapiFileUploadSingleResponse response = client.execute(request,AccessTokenUtil.getToken());
System.out.println(response.getBody());
#iAEHAqRmaWxlA6h5dW5kaXNrMATOCw_pYgXNBzAGzQNAB85dJrRkCM0BRA
*/
/* OapiCspaceAddToSingleChatRequest request = new OapiCspaceAddToSingleChatRequest();
request.setAgentId(DingConstant.AGENTID+"");
request.setUserid("0502101558117240531");
request.setMediaId("#iAEHAqRmaWxlA6h5dW5kaXNrMATOCw_pYgXNBzAGzQNAB85dJrRkCM0BRA");
request.setFileName("test.jpg");
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/cspace/add_to_single_chat?"+WebUtils.buildQuery(request.getTextParams(),"utf-8"));
OapiCspaceAddToSingleChatResponse response = client.execute(request, AccessTokenUtil.getToken());
System.out.println(response.getBody());*/
// saveFile("18c04469f269347386a232eb55304f7b","0502101558117240531","D:\\vhr.sql");
}
public static String uploadTrunk(){
String uploadId = uploadTransactionBegin(1000L);
uploadChunk(uploadId);
String mediaId = uploadTransactionEnd(uploadId);
return mediaId;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy