700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 企业微信更换头像

企业微信更换头像

时间:2020-09-24 13:25:13

相关推荐

企业微信更换头像

企业微信更换头像

步骤:

1、appid、应用secret

2、获取access_token

3、根据access_token调用企微上传媒体库接口

4、根据人员标识字段更换人员头像

import com.alibaba.fastjson.JSONObject;import com.aliyuncs.utils.IOUtils;import com.mehow.hklink.service.impl.QywxLinkServiceImpl;import com.mehow.hklink.utils.maycur.HttpRequest;import mons.codec.Charsets;import mons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.*;import .HttpURLConnection;import .URL;public class WxLinkUtil {final static String APPID="xxxxxxxx";/*** 企业微信应用secret*/final static String ORGSECRT="xxxxxxxx";private static Logger log = LoggerFactory.getLogger(WxLinkUtil.class);private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";/*** AccessToken获取* @param* @throws IOException* @throws NoSuchAlgorithmException* @throws NoSuchProviderException* @throws KeyManagementException*/public static JSONObject getOrgAccessToken() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException{// 微信secret 业务//String requestTokenUrl = "https://qyapi./cgi-bin/gettoken?corpid="+APPID+"&corpsecret="+ORGSECRT+"";// requestTokenUrl = requestTokenUrl.replace("APPID", APPID);// requestTokenUrl = requestTokenUrl.replace("SECRET", ORGSECRT);StringBuffer jsonObjectToken = CommonHttpUtil.httpsRequest("https://qyapi./cgi-bin/gettoken?corpid="+APPID+"&corpsecret="+ORGSECRT+"", "GET", null);// JSONObject jsonObject = JSONObject.parseObject(jsonObjectToken.toString());// System.out.println("---jsonObject-->>"+jsonObject);return JSONObject.parseObject(jsonObjectToken.toString());}/*** 上传临时素材* 素材上传得到media_id,该media_id仅三天内有效* media_id在同一企业内应用之间可以共享* 图片(image):10MB,支持JPG,PNG格式* 语音(voice) :2MB,播放长度不超过60s,仅支持AMR格式* 视频(video) :10MB,支持MP4格式* 普通文件(file):20MB* @param access_token* @param file 需要上传的文件* @param type 文件类型* @throws IOException*/public static String uploadMedia(String access_token , File file, String type,String params) throws IOException {String url = "https://qyapi./cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";url = url.replace("ACCESS_TOKEN",access_token).replace("TYPE",type);URL urlGet = new URL(url);HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection();conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);conn.setRequestMethod("POST");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", DEFAULT_USER_AGENT);conn.setRequestProperty("Charsert", "UTF-8");// 定义数据分隔线String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL";conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);OutputStream out = new DataOutputStream(conn.getOutputStream());// 定义最后数据分隔线StringBuilder mediaData = new StringBuilder();mediaData.append("--").append(BOUNDARY).append("\r\n");mediaData.append("Content-Disposition: form-data;name=\"media\";filename=\"" + file.getName() + "\"\r\n");mediaData.append("Content-Type:application/octet-stream\r\n\r\n");byte[] mediaDatas = mediaData.toString().getBytes();out.write(mediaDatas);DataInputStream fs = new DataInputStream(new FileInputStream(file));int bytes = 0;byte[] bufferOut = new byte[1024];while ((bytes = fs.read(bufferOut)) != -1) {out.write(bufferOut, 0, bytes);}IOUtils.closeQuietly(fs);// 多个文件时,二个文件之间加入这个out.write("\r\n".getBytes());if (StringUtils.isNotEmpty(params)) {StringBuilder paramData = new StringBuilder();paramData.append("--").append(BOUNDARY).append("\r\n");paramData.append("Content-Disposition: form-data;name=\"description\";");byte[] paramDatas = paramData.toString().getBytes();out.write(paramDatas);out.write(params.getBytes(Charsets.UTF_8));}byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();out.write(end_data);out.flush();IOUtils.closeQuietly(out);// 定义BufferedReader输入流来读取URL的响应InputStream in = conn.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));String valueString = null;StringBuffer bufferRes = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null) {bufferRes.append(valueString);}IOUtils.closeQuietly(in);// 关闭连接if (conn != null) {conn.disconnect();}log.info("返回信息:"+bufferRes.toString());return bufferRes.toString();}/*** 根据员工工号,上传指定照片到企业微信临时素材库** 素材上传得到media_id,该media_id仅三天内有效** media_id在同一企业内应用之间可以共享** 图片(image):10MB,支持JPG,PNG格式** 语音(voice) :2MB,播放长度不超过60s,仅支持AMR格式** 视频(video) :10MB,支持MP4格式** 普通文件(file):20MB* @param access_token token* @param ins 上传文件的inputstream流* @param type 文件类型* @param params* @param code 员工工号* @return* @throws IOException*/public static String uploadMedia2(String access_token , InputStream ins, String type,String params,String code) throws IOException {String url = "https://qyapi./cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";url = url.replace("ACCESS_TOKEN",access_token).replace("TYPE",type);URL urlGet = new URL(url);HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection();conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);conn.setRequestMethod("POST");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", DEFAULT_USER_AGENT);conn.setRequestProperty("Charsert", "UTF-8");// 定义数据分隔线String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL";conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);OutputStream out = new DataOutputStream(conn.getOutputStream());// 定义最后数据分隔线StringBuilder mediaData = new StringBuilder();mediaData.append("--").append(BOUNDARY).append("\r\n");mediaData.append("Content-Disposition: form-data;name=\"media\";filename=\"" + code + "\"\r\n");mediaData.append("Content-Type:application/octet-stream\r\n\r\n");byte[] mediaDatas = mediaData.toString().getBytes();out.write(mediaDatas);DataInputStream fs = new DataInputStream(ins);int bytes = 0;byte[] bufferOut = new byte[1024];while ((bytes = fs.read(bufferOut)) != -1) {out.write(bufferOut, 0, bytes);}IOUtils.closeQuietly(fs);// 多个文件时,二个文件之间加入这个out.write("\r\n".getBytes());if (StringUtils.isNotEmpty(params)) {StringBuilder paramData = new StringBuilder();paramData.append("--").append(BOUNDARY).append("\r\n");paramData.append("Content-Disposition: form-data;name=\"description\";");byte[] paramDatas = paramData.toString().getBytes();out.write(paramDatas);out.write(params.getBytes(Charsets.UTF_8));}byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();out.write(end_data);out.flush();IOUtils.closeQuietly(out);// 定义BufferedReader输入流来读取URL的响应InputStream in = conn.getInputStream();BufferedReader read = new BufferedReader(new InputStreamReader(in, Charsets.UTF_8));String valueString = null;StringBuffer bufferRes = null;bufferRes = new StringBuffer();while ((valueString = read.readLine()) != null) {bufferRes.append(valueString);}IOUtils.closeQuietly(in);// 关闭连接if (conn != null) {conn.disconnect();}log.info("返回信息:"+bufferRes.toString());return bufferRes.toString();}/*** 更新企业微信人员头像* 根据员工工号 更新指定人员企业微信头像* @param mediaId 微信临时素材库图片id* @param code 工号* @return*/public static String updateQwPersonTouxiang(String access_token,String mediaId,String code){String requstUrl = "https://qyapi./cgi-bin/user/update?access_token=ACCESS_TOKEN";requstUrl = requstUrl.replace("ACCESS_TOKEN",access_token);String resMsg = "";try {JSONObject body = new JSONObject();body.put("userid",code);//企业微信uerid=工号body.put("avatar_mediaid",mediaId);resMsg = HttpRequest.sendPost(requstUrl, body.toJSONString(), null);} catch (Exception e) {e.printStackTrace();resMsg = "请求失败,请检查。"+e;}log.info(resMsg);return resMsg;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。