临时素材
(素材类型只有这几种,且上传方法一致,只以上传图片方法为例)
https://api.weixin.qq.com/cgi-bin/media/upload?access_token=
ACCESS_TOKEN&type=TYPE
//临时素材接口路径
static final String SET_TEMPORARY_MATERIAL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=";
//调用接口凭证
String ACCESS_TOKEN = "13_ajKU3NczC5pYaXNWH1Avv-dtiiuqNXC_k3RCEwXgmuutHhQS9JlZZJ-U3HdBXGWAXa1pfXXuk_Zw-ezWv6NoGv_jeRaKTcHxt3ZV5nsAzMCrJ85ASTDhGCUb4yyWvPmWwVImMx7qyxdUCW7KFLBdACAJPF";
HttpURLConnection huct = (HttpURLConnection) new URL(SET_TEMPORARY_MATERIAL + ACCESS_TOKEN+"&type=image").openConnection();
huct.setRequestMethod("POST");//注意,只能以POST方式上传文件
huct.setDoOutput(true);//设置可以有返回值
File file = new File("云烟成雨.jpg");
huct.setRequestProperty("Content-Type", "multipart/form-data; boundary=-------------------146043902153");
OutputStream os = huct.getOutputStream();
StringBuilder strb = new StringBuilder();
//设置头部分割符
strb.append("\r\n---------------------146043902153\r\n");
strb.append("Content-Disposition:form-data;name=\"media\";");
strb.append("filename=\""+file.getName()+"\";");//图片名称
strb.append("Content-Type:\"image\";encoding=utf-8;");//image媒体文件类型和设置编码格式
strb.append("filelength:\"" + file.length() + "\"\r\n");//文件大小
strb.append("application/octet-stream\r\n\r\n");
os.write(strb.toString().getBytes("UTF-8"));
FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[fis.available()];
fis.read(bt);
fis.close();
os.write(bt);
//尾部分割符
os.write("\r\n---------------------146043902153--\r\n\r\n".getBytes());
os.flush();
os.close();
InputStream is = huct.getInputStream();
byte[] by = new byte[is.available()];
is.read(by);
System.out.println(new String(by));//返回数据
永久素材
(与临时素材只有三个区别,1.接口不同,2.上传视频类素材方法不同,3.永久图文素材)
https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE
//永久素材接口路径
static final String SET_PERMANENT_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=";
//调用接口凭证
String ACCESS_TOKEN = "13_ajKU3NczC5pYaXNWH1Avv-dtiiuqNXC_k3RCEwXgmuutHhQS9JlZZJ-U3HdBXGWAXa1pfXXuk_Zw-ezWv6NoGv_jeRaKTcHxt3ZV5nsAzMCrJ85ASTDhGCUb4yyWvPmWwVImMx7qyxdUCW7KFLBdACAJPF";
HttpURLConnection huct = (HttpURLConnection) new URL(SET_TEMPORARY_MATERIAL + ACCESS_TOKEN+"&type=video").openConnection();
huct.setRequestMethod("POST");//注意,只能以POST方式上传文件
huct.setDoOutput(true);//设置可以有返回值
File file = new File("终级斩杀.mp4");
huct.setRequestProperty("Content-Type", "multipart/form-data; boundary=-------------------146043902153");
OutputStream os = huct.getOutputStream();
StringBuilder strb = new StringBuilder();
//设置头部分割符
strb.append("\r\n---------------------146043902153\r\n");
strb.append("Content-Disposition:form-data;name=\"media\";");
strb.append("filename=\""+file.getName()+"\";");//图片名称
strb.append("Content-Type:\"video\";encoding=utf-8;");//video媒体文件类型和设置编码格式
strb.append("filelength:\"" + file.length() + "\"\r\n");//文件大小
strb.append("application/octet-stream\r\n\r\n");
os.write(strb.toString().getBytes("UTF-8"));
FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[fis.available()];
fis.read(bt);
fis.close();
os.write(bt);
String perpetual = "";//创建永久视频素材时,要额外添加表单数据
perpetual += "\r\n---------------------146043902153\r\n";
perpetual += "Content-Disposition:form-data;name=\"description\";\r\n\r\n";
perpetual += ("{\"title\":\"视频素材的标题\",\"introduction\":\"视频素材的描述\"}");
os.write(perpetual.getBytes("UTF-8"));
//尾部分割符
os.write("\r\n---------------------146043902153--\r\n\r\n".getBytes());
os.flush();
os.close();
InputStream is = huct.getInputStream();
byte[] by = new byte[is.available()];
is.read(by);
System.out.println(new String(by));//返回数据
永久图文素材(待补充)
https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN
上传图文消息内的图片获取URL
https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN
版权声明:本文为qq_39652227原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。