zoukankan      html  css  js  c++  java
  • 微信公众号新增图片类型永久素材

    新增其他类型永久素材

    感谢博主:

    https://blog.csdn.net/yh88356656/article/details/50685599

    https://www.cnblogs.com/jeffen/p/6937788.html

    接口调用请求说明

    通过POST表单来调用接口,表单id为media,包含需要上传的素材内容,有filename、filelength、content-type等信息。请注意:图片素材将进入公众平台官网素材管理模块中的默认分组。

    http请求方式: POST,需使用https
    https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE
    调用示例(使用curl命令,用FORM表单方式新增一个其他类型的永久素材,curl命令的使用请自行查阅资料)

    参数说明

    参数是否必须说明
    access_token 调用接口凭证
    type 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
    media form-data中媒体文件标识,有filename、filelength、content-type等信息

    注意:media包含你文件的两个信息{文件名,文件大小},通过File file = new File(filepath);

    filename=file.getName();

    filelength=file.length();

    具体使用参考以下http请求代码

    public class UploadPictureFile {

    /**
    * 上传其他永久素材(图片素材的上限为5000,其他类型为1000)
    * @param appid
    * @param secret
    * @return
    * @throws Exception
    */
    public static JSONObject addMaterialImage(File file,String type) throws Exception {
    try {
    System.out.println("开始上传"+type+"永久素材---------------------");
    AccessToken accessTokenObj = WeixinUtil.getAccessToken();
    //开始获取证书
    String accessToken=accessTokenObj.getToken();
    if(StringUtils.isBlank(accessToken)){
    System.out.println("accessToken is null");
    return null;
    }
    //上传素材
    //HPb4PQugmc1j0Dxhf7_9U4c3MhGHzvkJGhi7Yi6GZ2s
    String path="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+accessToken+"&type="+type;
    String result=FileUploadHttpRequest.connectHttpsByPost(path, file);
    result=result.replaceAll("[\\]", "");
    JSONObject resultJSON=JSONObject.fromObject(result);
    if(resultJSON!=null){
    if(resultJSON.get("media_id")!=null){
    System.out.println(resultJSON.get("media_id").toString());
    System.out.println("上传"+type+"永久素材成功");
    return resultJSON;
    }else{
    System.out.println("上传"+type+"永久素材失败");
    }
    }
    return null;
    } catch (Exception e) {
    System.out.println("程序异常---"+e);
    throw e;
    }finally{
    System.out.println("结束上传"+type+"永久素材---------------------");
    }
    }

    public static void main(String[] args) {
    try {
      addMaterialImage(new File("D:/0.jpg"),"image");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }


    }

    http请求:

    public class FileUploadHttpRequest {
    public static String connectHttpsByPost(String path,File file) throws IOException{
    URL url = new URL(path);
    HttpURLConnection con=(HttpURLConnection)(url.openConnection());
    String result =null;
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false); // post方式不能使用缓存
    // 设置请求头信息
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Charset", "UTF-8");
    // 设置边界
    String BOUNDARY = "----------" + System.currentTimeMillis();
    con.setRequestProperty("Content-Type",
    "multipart/form-data; boundary="
    + BOUNDARY);
    // 请求正文信息
    // 第一部分:
    StringBuilder sb = new StringBuilder();
    sb.append("--"); // 必须多两道线
    sb.append(BOUNDARY);
    sb.append(" ");
    sb.append("Content-Disposition: form-data;name="media";filelength=""+file.length()+"";filename=""
    + file.getName() + "" ");
    sb.append("Content-Type:application/octet-stream ");
    byte[] head = sb.toString().getBytes("utf-8");
    // 获得输出流
    OutputStream out = new DataOutputStream(con.getOutputStream());
    // 输出表头
    out.write(head);
    // 文件正文部分
    // 把文件已流文件的方式 推入到url中
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    int bytes = 0;
    byte[] bufferOut = new byte[1024];
    while ((bytes = in.read(bufferOut)) != -1) {
    out.write(bufferOut, 0, bytes);
    }
    in.close();
    // 结尾部分
    byte[] foot = (" --" + BOUNDARY + "-- ").getBytes("utf-8");// 定义最后数据分隔线
    out.write(foot);
    out.flush();
    out.close();
    StringBuffer buffer = new StringBuffer();
    BufferedReader reader = null;
    try {
    // 定义BufferedReader输入流来读取URL的响应
    reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String line = null;
    while ((line = reader.readLine()) != null) {
    buffer.append(line);
    }
    if (result == null) {
    result = buffer.toString();
    }
    } catch (IOException e) {
    System.out.println("发送POST请求出现异常!" + e);
    e.printStackTrace();
    throw new IOException("数据读取异常");
    } finally {
    if (reader != null) {
    reader.close();
    }

    }
    return result;
    };
    }

  • 相关阅读:
    Nagios显示器mysql定从库: libmysqlclient.so.18: cannot open shared object file: No such
    UVA 11402
    Oracle 11g 的PL/SQL函数结果缓存
    最大公约数(Greatest Common Divisor)
    计时器 Timer
    ArcGIS Engine 捕捉
    AE+C# 图层中增加相应属性标注
    C# ComboBox自动完成功能的示例
    Visual Assist的破解与安装
    GitHub的代码托管和使用方法
  • 原文地址:https://www.cnblogs.com/mjbenkyo/p/9122490.html
Copyright © 2011-2022 走看看