- /**
- * 上传其他永久素材(图片素材的上限为5000,其他类型为1000)
- * @param appid
- * @param secret
- * @return
- * @throws Exception
- */
- public JSONObject addMaterialEver(String appid, String secret,File file,String type) throws Exception {
- try {
- PrintUtil.println("开始上传"+type+"永久素材---------------------");
- //开始获取证书
- String accessToken=new AccessTokenBizImpl().getAccessToken(appid, secret);
- if(!StringUtil.isEmptyString(accessToken)){
- PrintUtil.println("accessToken is null");
- return null;
- }
- //上传素材
- String path="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+accessToken+"&type="+type;
- String result=HttpsUtil.connectHttpsByPost(path, null, file);
- result=result.replaceAll("[\\\\]", "");
- PrintUtil.println("result:"+result);
- JSONObject resultJSON=JSONObject.fromObject(result);
- if(resultJSON!=null){
- if(resultJSON.get("media_id")!=null){
- PrintUtil.println("上传"+type+"永久素材成功");
- return resultJSON;
- }else{
- PrintUtil.println("上传"+type+"永久素材失败");
- }
- }
- return null;
- } catch (Exception e) {
- PrintUtil.println("程序异常", e);
- throw e;
- }finally{
- PrintUtil.println("结束上传"+type+"永久素材---------------------");
- }
- }
- /**
- * 上传图片到微信服务器(本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下)
- * @param appid
- * @param secret
- * @param type
- * @param file
- * @return
- * @throws Exception
- */
- public JSONObject addMaterialEver(String appid, String secret,File file) throws Exception {
- try {
- PrintUtil.println("开始上传图文消息内的图片---------------------");
- //开始获取证书
- String accessToken=new AccessTokenBizImpl().getAccessToken(appid, secret);
- if(!StringUtil.isEmptyString(accessToken)){
- PrintUtil.println("accessToken is null");
- return null;
- }
- //上传图片素材
- String path="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="+accessToken;
- String result=HttpsUtil.connectHttpsByPost(path, null, file);
- result=result.replaceAll("[\\\\]", "");
- PrintUtil.println("result:"+result);
- JSONObject resultJSON=JSONObject.fromObject(result);
- if(resultJSON!=null){
- if(resultJSON.get("url")!=null){
- PrintUtil.println("上传图文消息内的图片成功");
- return resultJSON;
- }else{
- PrintUtil.println("上传图文消息内的图片失败");
- }
- }
- return null;
- } catch (Exception e) {
- PrintUtil.println("程序异常", e);
- throw e;
- }finally{
- PrintUtil.println("结束上传图文消息内的图片---------------------");
- }
- }
具体的HTTPS请求: