Java微信公众平台之素材管理

网友投稿 437 2023-02-05


Java微信公众平台之素材管理

微信素材管理和群发这块文档对java很不友好。本文只对新增临时素材,新增永久素材做介绍,其余获取、删除、修改自行补充

素材的限制

图片(image): 2M,支持PNG\JPEG\JPG\GIF格式

语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式

视频(video):10MB,支持MP4格式

缩略图(thumb):64KB,支持JPG格式

一、新增临时素材

接口:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE,再传一个媒体文件类型,可以是图片(image)、语音(voice)、视频(video)和缩略图(thumb)。

1、订阅号和服务号要通过认证

2、临时素材media_id是可复用的

3、媒体文件在微信后台保存时间为3天,即3天后media_id失效。

/**

* 上传临时素材(本地)

*

* @param accessToken

* @param type

* 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)

* @param path

* 图片路径

* @return

*/

public static UploadMediasResult uploadTempMediaFile(String accessToken, String type, String path) {

UploadMediasResult result = null;

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

params.put("type", type);

try {

String json = HttpsUploadMediaFile(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_TEMP_MEDIA_TYPE_URL,

params, path);

result = JsonUtil.fromJsonString(json, UploadMediasResult.class);

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 上传临时素材(网络)

*

* @param accessToken

* @param type

* 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)

* @param path

* 图片路径

* @return

*/

public static UploadMediasResult uploadTempMedia(String accessToken, String type, String path) {

UploadMediasResult result = null;

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

params.put("type", type);

try {

String json = HttpsUploadMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_TEMP_MEDIA_TYPE_URL, params,

path, 0, 0);

result = JsonUtil.fromJsonString(json, UploadMediasResult.class);

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

二、新增永久素材

接口:https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE,媒体文件类型,分别有图片(image)、语音(voice)、视频(video,例外)和缩略图(thumb)

/**

* 上传永久素材(本地)

*

* @param accessToken

* @param type

* 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)

* @return

*/

public static UploadMediasResult uploadForeverMediaFile(String accessToken, String type, String path) {

UploadMediasResult result = null;

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

params.put("type", type);

try {

String json = HttpsUploadMediaFile(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL,

params, path);

result = JsonUtil.fromJsonString(json, UploadMediasResult.class);

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* 上传永久素材(网络)

*

* @param accessToken

* @param type

* 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)

* @return

*/

public static UploadMediasResult uploadForeverMedia(String accessToken, String type, String path) {

UploadMediasResult result = null;

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

params.put("type", type);

try {

String json = HttpsUploadMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL, params,

path, 0, 0);

result = JsonUtil.fromJsonString(json, UploadMediasResult.class);

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

新增永久视频素材需特别注意,在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息title和introduction,内容格式为JSON

/**

* 上传永久素材(video)

*

* @param accessToken

* @return

*/

public static String uploadForeverMediaFile(String accessToken, String title, String introduction, String path) {

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

params.put("type", "video");

String mediaId = null;

try {

String json = HttpsUploadVideoMediaFile(SystemConfig.POST_METHOD,

WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL, params, path, title, introduction);

mediaId = JsonUtil.fromJsonString(json, "media_id");

} catch (Exception e) {

e.printStackTrace();

}

return mediaId;

}

/**

* 上传永久素材(video,网络)

*

* @param accessToken

* @return

*/

public static String uploadForeverMedia(String accessToken, String title, String introduction, String path) {

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

params.put("type", "video");

String mediaId = null;

try {

String json = HttpsUploadVideoMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL,

params, path, title, introduction, 0, 0);

mediaId = JsonUtil.fromJsonString(json, "media_id");

} catch (Exception e) {

e.printStackTrace();

}

return mediaId;

}

三、新增永久图文素材

接口:https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN,post信息参见UploadNewsMedia 实体类

对于常用的素材,开发者可通过本接口上传到微信服务器,永久使用.

1、永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽)。

3、图文消息的具体内容中,微信后台将过滤外部的图片链接,图片url需通过"上传图文消息内的图片获取URL"接口上传图片获取。

/**

* 上传永久图文消息的素材

*

* @param accessToken

* 授权token

* @param entity

* 图文消息对象

* @return

*/

public static UploadMediasResult uploadNewsMedia(String accessToken, List entity) {

UploadMediasResult result = null;

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

// post 提交的参数

TreeMap> dataParams = new TreeMap>();

dataParams.put("articles", entity);

String data = JsonUtil.toJsonString(dataParams);

String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD,

WechatConfig.UPLOAD_FOREVER_NEWS_MEDIA_URL, params, data);

result = JsonUtil.fromJsonString(json, UploadMediasResult.class);

return result;

}

四、上传图文消息内的图片获取URL

接口:https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN

/**

* 上传图文消息内的图片获取URL(本地)

*

* @param accessToken

* @param path

* @return

*/

public static String uploadImgMediaFile(String accessToken, String path) {

TreeMap params = new TreeMap<>();

params.put("access_token", accessToken);

String url = null;

try {

String json = HttpsUploadMediaFile(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_IMG_MEDIA_URL, params,

path);

url = JsonUtil.fromJsonString(json, "url");

} catch (Exception e) {

e.printStackTrace();

}

return url;

}

/**

* 上传图文消息内的图片获取URL(网络)

*

* @param accessToken

* @param path

* @return

*/

public static String uploadImgMedia(String accessToken, String path) {

TreeMap params = new TreeMap();

params.put("access_token", accessToken);

String url = null;

try {

String json = HttpsUploadMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_IMG_MEDIA_URL, params, path, 0,

0);

url = JsonUtil.fromJsonString(json, "url");

} catch (Exception e) {

e.printStackTrace();

}

return url;

}

五、部分工具类

配置类

public static final String UPLOAD_IMG_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg";

public static final String UPLOAD_FOREVER_NEWS_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news";

public static final String UPLOAD_TEMP_MEDIA_TYPE_URL = "https://api.weixin.qq.com/cgi-bin/media/upload";

public static final String UPLOAD_FOREVER_MEDIA_TYPE_URL = "https://api.weixin.qq.com/cgi-bin/material/add_material";

上传图文消息素材返回类

package com.phil.wechat.msg.model.media;

/**

* 上传图文消息素材返回的结果

* @author phil

* @date 2017年9月20日

*

*/

public class UploadMediasResult {

private String type; // 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb),次数为news,即图文消息

private String media_id; // 媒体文件/图文消息上传后获取的唯一标识

private String created_at; // 媒体文件上传时间

}

上传图文消息素材实体类

上传方法

/**

* 上传媒体文件(本地)

*

* @param method

* 请求方法 GET/POST

* @param path

* api的路径

* @param param

* api参数

* @param mediaPath

* 待上传的image/music 的path

* @return

* @throws Exception

*/

public static String HttpsUploadMediaFile(String method, String path, Map param, String mediaPath)

throws Exception {

String result = null;

URL url = new URL(setParmas(param, path, ""));

OutputStream output = null;

DataInputStream inputStream = null;

try {

File file = new File(mediaPath);

if (!file.isFile() || !file.exists()) {

throw new IOException("file is not exist");

}

HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setDoInput(true);

con.setDoOuhttp://tput(true);

con.setUseCaches(false);

con.setRequestMethod(SystemConfig.POST_METHOD);

// 设置请求头信息

con.setRequestProperty("Connection", "Keep-Alive");

con.setRequestProperty("Charset", SystemConfig.DEFAULT_CHARACTER_ENCODING);

// 设置边界

String boundary = "----------" + System.currentTimeMillis();

con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

// 请求正文信息

// 第一部分

output = new DataOutputStream(con.getOutputStream());

IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write(("Content-Disposition: form-data;name=\"media\"; filename=\"" + file.getName() + "\"\r\n")

.getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write(

"Content-Type:application/octet-stream\r\n\r\n".getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING),

output);

// IOUtils.write(("Content-Type: "+ fileExt + "\r\n\r\n").getBytes(), output);

// 文件正文部分

// 把文件已流文件的方式 推入到url中

inputStream = new DataInputStream(new FileInputStream(file));

IOUtils.copy(inputStream, output);

// 结尾部分

IOUtils.write(("\r\n--" + boundary + "--\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

output.flush();

result = inputStreamToString(con.getInputStream());

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (ProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

throw new IOException("read data error");

} finally {

IOUtils.closeQuietly(output);

IOUtils.closeQuietly(inputStream);

}

return result;

}

/**

* 上传媒体文件(不能本地)

*

* @param method

* 请求方法 GET/POST

* @param path

* api的路径

* @param param

* api参数

* @param mediaPath

* 待上传的image/music 的path

* @param connTime

* 连接时间 默认为5000

* @param readTime

* 读取时间 默认为5000

* @return

* @throws Exception

*/

public static String HttpsUploadMedia(String method, String path, Map param, String mediaPath,

int connTime, int readTime) throws Exception {

String result = "";

URL url = new URL(setParmas(param, path, ""));

OutputStream output = null;

BufferedInputStream inputStream = null;

try {

String boundary = "----";

HttpURLConnection conn = getConnection(method, url);

conn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime);

conn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime);

conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

output = conn.getOutputStream();

URL mediaUrl = new URL(mediaPath);

if (mediaUrl != null) {

HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl.openConnection();

mediaConn.setDoOutput(true);

mediaConn.setUseCaches(false);

mediaConn.setRequestMethod(SystemConfig.GET_METHOD);

mediaConn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime);

mediaConn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime);

String connType = mediaConn.getContentType();

// 获得文件扩展

String fileExt = getFileExt(connType);

IOUtils.write(("--" + boundary + "\r\n").getBytes(), output);

IOUtils.write(("Content-Disposition: form-data; name=\"media\"; filename=\"" + getFileName(mediaPath)

+ "\"\r\n").getBytes(), output);

IOUtils.write(("Content-Type: " + fileExt + "\r\n\r\n").getBytes(), output);

inputStream = new BufferedInputStream(mediaConn.getInputStream());

IOUtils.copy(inputStream, output);

IOUtils.write(("\r\n----" + boundary + "--\r\n").getBytes(), output);

mediaConn.disconnect();

// 获取输入流

result = inputStreamToString(conn.getInputStream());

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (ProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

IOUtils.closeQuietly(output);

IOUtils.closeQuietly(inputStream);

}

return result;

}

/**

* 上传Video媒体文件(本地)

*

* @param method

* 请求方法 GET/POST

* @param path

* api的路径

* @param param

* api参数

* @param mediaPath

* 待上传的voide 的path

* @param title

* 视频标题

* @param introduction

* 视频描述

* @return

* @throws Exception

*/

public static String HttpsUploadVideoMediaFile(String method, String path, Map param,

String mediaPath, String title, String introduction) throws Exception {

String result = null;

URL url = new URL(setParmas(param, path, ""));

OutputStream output = null;

DataInputStream inputStream = null;

try {

File file = new File(mediaPath);

if (!file.isFile() || !file.exists()) {

throw new IOException("file is not exist");

}

HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setDoInput(true);

con.setDoOutput(true);

con.setUseCaches(false);

con.setRequestMethod(SystemConfig.POST_METHOD);

// 设置请求头信息

con.setRequestProperty("Connection", "Keep-Alive");

con.setRequestProperty("Charset", SystemConfig.DEFAULT_CHARACTER_ENCODING);

// 设置边界

String boundary = "----------" + System.currentTimeMillis();

con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

// 请求正文信息

// 第一部分

output = new DataOutputStream(con.getOutputStream());

IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write(("Content-Disposition: form-data;name=\"media\"; filename=\"" + file.getName() + "\"\r\n")

.getBytes(), output);

IOUtils.write("Content-Type: video/mp4 \r\n\r\n".getBytes(), output);

// 文件正文部分

// 把文件已流文件的方式 推入到url中

inputStream = new DataInputStream(new FileInputStream(file));

IOUtils.copy(inputStream, output);

// 结尾部分

IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n"

.getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write(("{\"title\":\"" + title + "\",\"introduction\":\"" + introduction + "\"}")

.getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtihttp://ls.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING),

output);

output.flush();

result = inputStreamToString(con.getInputStream());

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (ProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

throw new IOException("read data error");

} finally {

IOUtils.closeQuietly(output);

IOUtils.closeQuietly(inputStream);

}

return result;

}

/**

* 上传Video媒体文件(网络)

*

* @param method

* 请求方法 GET/POST

* @param path

* api的路径

* @param param

* api参数

* @param mediaPath

* 待上传的voide 的path

* @param title

* 视频标题

* @param introduction

* 视频描述

* @param connTime

* 连接时间 默认为5000

* @param readTime

* 读取时间 默认为5000

* @return

* @throws Exception

*/

public static String HttpsUploadVideoMedia(String method, String path, Map param, String mediaPath,

String title, String introduction, int connTime, int readTime) throws Exception {

String result = null;

URL url = new URL(setParmas(param, path, ""));

OutputStream output = null;

BufferedInputStream inputStream = null;

try {

String boundary = "----";

HttpURLConnection conn = getConnection(method, url);

conn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime);

conn.setReadTimeout(readTime == 0 ? DEFAhttp://ULT_UPLOAD_READTIME : readTime);

conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

output = conn.getOutputStream();

URL mediaUrl = new URL(mediaPath);

if (mediaUrl != null) {

HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl.openConnection();

mediaConn.setDoOutput(true);

mediaConn.setUseCaches(false);

mediaConn.setRequestMethod(SystemConfig.GET_METHOD);

mediaConn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime);

mediaConn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime);

IOUtils.write(("--" + boundary + "\r\n").getBytes(), output);

IOUtils.write(("Content-Disposition: form-data; name=\"media\"; filename=\"" + getFileName(mediaPath)

+ "\"\r\n").getBytes(), output);

IOUtils.write("Content-Type: video/mp4 \r\n\r\n".getBytes(), output);

inputStream = new BufferedInputStream(mediaConn.getInputStream());

IOUtils.copy(inputStream, output);

// 结尾部分

IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n"

.getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write(("{\"title\":\"" + title + "\",\"introduction\":\"" + introduction + "\"}")

.getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output);

IOUtils.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING),

output);

mediaConn.disconnect();

// 获取输入流

result = inputStreamToString(conn.getInputStream());

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (ProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

throw new IOException("read data error");

} finally {

IOUtils.closeQuietly(output);

IOUtils.closeQuietly(inputStream);

}

return result;

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解重试框架Spring retry实践
下一篇:国产机连接共享文件夹的软件(国产机连接共享文件夹的软件叫什么)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~