概述
@Override
public void createExportTask(String fileName, String stringFile, List list, AsyncExportDTO exportDTO, Boolean isTemplate, String templateUrl, Class tclass) {
//加载Excel 生成Excel
ExcelWriter excelWriter = null;
try {
if (isTemplate) {
File file = ExcelUtil.getTemplateFile(templateUrl);
excelWriter = EasyExcel.write(stringFile).withTemplate(file).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(list, writeSheet);
} else {
excelWriter = EasyExcel.write(stringFile).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0, fileName).head(tclass).build();
excelWriter.write(list, writeSheet);
}
excelWriter.finish();
} catch (Exception e) {
e.printStackTrace();
}
//获取企业微信 accesstoken
Result<String> accessToken = qwApiFeign.getAccessToken(CommonConstant.AGENT_SAAS, TenantContextHolder.getTenantId());
log.info("accessToken:{}", accessToken.getData());
//Excel 取出file 对象
assert excelWriter != null;
File file = excelWriter.writeContext().writeWorkbookHolder().getFile();
RestTemplate restTemplate = new RestTemplate();
try {
String wxUrl = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
String url = wxUrl.replace("ACCESS_TOKEN", accessToken.getData()).replace("TYPE", "file");
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
headers.setContentLength(file.length());
headers.setContentDispositionFormData("media", file.getName());
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
FileSystemResource resource = new FileSystemResource(file.getPath());
param.add("file", resource);
HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
ResponseEntity<String> data = restTemplate.postForEntity(url, formEntity, String.class);
JSONObject jsonObject = JSONObject.parseObject(data.getBody());
String mediaId = jsonObject.getString("media_id");
log.info("企业微信返回media_id:{}", mediaId);
// GC
if (file.exists()) {
FileInputStream inputStream = new FileInputStream(file);
inputStream.close();
file.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
拿到mediaid 到 企业微信api
点击下载
最后
以上就是笑点低背包为你收集整理的企业微信上传临时素材的全部内容,希望文章能够帮你解决企业微信上传临时素材所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复