java微信公众号上传永久素材,微信公众号开发-永久素材的上传
几种素材的上传方式
微信服务器素材上传
上传素材得到返回JSON
调用示例(使用curl命令,用FORM表单方式上传一个图片):
curl -F media=@test.jpg “https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN”
直接上代码-_-
/**
* 微信服务器素材上传
*
* @param file
* 表单名称media
* @param token
* access_token
* @param type
* type只支持四种类型素材(video/image/voice/thumb)
* @param upurl
* 使用微信提供的接口即可
*/
public static JSONObject uploadMedia(File file, String token, String type, String upurl) {
if (file == null || token == null || type == null) {
return null;
}
if (!file.exists()) {
logger.info("上传文件不存在,请检查!");
return null;
}
String url = upurl;
JSONObject jsonObject = null;
PostMethod post = new PostMethod(url);
post.setRequestHeader("Connection", "Keep-Alive");
post.setRequestHeader("Cache-Control", "no-cache");
FilePart media = null;
HttpClient httpClient = new HttpClient();
// 信任任何类型的证书
Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myhttps);
try {
media = new FilePart("media", file);
Part[] parts = new Part[] { new StringPart("access_token", token), new StringPart("type", type), media };
MultipartRequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
post.setRequestEntity(entity);
int status = httpClient.executeMethod(post);
if (status == HttpStatus.SC_OK) {
String text = post.getResponseBodyAsString();
jsonObject = JSONObject.fromObject(text);
} else {
logger.info("upload Media failure status is:" + status);
}
} catch (FileNotFoundException execption) {
execption.printStackTrace();
} catch (HttpException execption) {
execption.printStackTrace();
} catch (IOException execption) {
execption.printStackTrace();
}
return jsonObject;
}
上传其他类型永久素材,模拟form表单的形式
// 素材上传(POST)
规定上传地址
public static final String UPLOAD_MEDIA = “https://api.weixin.qq.com/cgi-bin/material/add_material”;
/**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
*
* @param url
* 请求地址 form表单url地址
* @param type
* 上传类型
* @param title
* 视频标题
* @param introduction
* 视频描述
* @return
*/
public static JSONObject uploadVideo(File file, String type, String title, String introduction, String token) {
String url = UPLOAD_MEDIA + "?access_token=" + token + "&type=" + type;
String result = null;
JSONObject jsonObject = null;
try {
URL uploadURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) uploadURL.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");
String boundary = "-----------------------------" + System.currentTimeMillis();
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream output = conn.getOutputStream();
output.write(("--" + boundary + "\r\n").getBytes());
output.write(
String.format("Content-Disposition: form-data; name=\"media\"; filename=\"%s\"\r\n", file.getName())
.getBytes());
output.write("Content-Type: video/mp4 \r\n\r\n".getBytes());
byte[] data = new byte[1024];
int len = 0;
FileInputStream input = new FileInputStream(file);
while ((len = input.read(data)) > -1) {
output.write(data, 0, len);
}
/* 对类型为video的素材进行特殊处理 */
if ("video".equals(type)) {
output.write(("--" + boundary + "\r\n").getBytes());
output.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n".getBytes());
output.write(
String.format("{\"title\":\"%s\", \"introduction\":\"%s\"}", title, introduction).getBytes());
}
output.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes());
output.flush();
output.close();
input.close();
InputStream resp = conn.getInputStream();
StringBuffer sb = new StringBuffer();
while ((len = resp.read(data)) > -1)
sb.append(new String(data, 0, len, "utf-8"));
resp.close();
result = sb.toString();
jsonObject = JSONObject.fromObject(result);
} catch (IOException e) {
// ....
}
return jsonObject;
}
得到返回json即可。
多媒体下载接口
// 素材下载接口:不支持视频文件的下载(GET)
private static final String DOWNLOAD_MEDIA = “http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s”;
使用String.format() 替换对应%s即可
这里直接将得到的相应输出到文件File中
/**
* 以http方式发送请求,并将请求响应内容输出到文件
*
* @param path
* 请求路径
* @param method
* 请求方法
* @param body
* 请求数据
* @return 返回响应的存储到文件
*/
public static File httpRequestToFile(String fileName, String path, String method, String body) {
if (fileName == null || path == null || method == null) {
return null;
}
File file = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
FileOutputStream fileOut = null;
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(method);
if (null != body) {
OutputStream outputStream = conn.getOutputStream();
outputStream.write(body.getBytes("UTF-8"));
outputStream.close();
}
inputStream = conn.getInputStream();
if (inputStream != null) {
file = new File(fileName);
} else {
return file;
}
// 写入到文件
fileOut = new FileOutputStream(file);
if (fileOut != null) {
int c = inputStream.read();
while (c != -1) {
fileOut.write(c);
c = inputStream.read();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
/*
* 必须关闭文件流 否则JDK运行时,文件被占用其他进程无法访问
*/
try {
inputStream.close();
fileOut.close();
} catch (IOException execption) {
execption.printStackTrace();
}
}
return file;
}
java微信公众号上传永久素材,微信公众号开发-永久素材的上传相关推荐
- 使用java spring开发ckeditor的文件上传功能(转)
说明:原帖提供的代码无法直接运行.本人在原帖基础上做了一些修改,修复了一些bug. 关于CKEditor的使用,网络上有无数的文章,这里不再赘述.而关于java支持的文件上传功能,网络上同样有千千万万 ...
- 小狐狸横版游戏开发学习笔记(上)
小狐狸横版游戏开发学习笔记(上) 目录 小狐狸横版游戏开发学习笔记(上) 1.关于如何创建Tilemap 2.关于地图格子之间出现间隙的问题 3.如何设置自己想要的控制按键 4.如何解决玩家移动过程中 ...
- php上传公众号临时素材-微信开发素材管理6
微信公众平台中, 临时素材是一种重要的素材, 它的特点是: 1. 没有数量限制,你可以上传任意数量的临时素材 2. 微信服务器保存时间短,只有3天,到期后media_id就不能使用了 这一节课程中, ...
- 微信公众开放平台开发03---百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试
微信公众开放平台开发03---百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试 技术qq交流群:JavaDream ...
- 微信公众号数据2019_如何制作微信公众号图文素材 微信公众号采集器好用吗
现在有很多人都会通过微信公众号来发布文章.图片,这时候就需要使用一些编辑技巧了.下面拓途数据就和大家一同来看看如何制作微信公众号图文素材,微信公众号采集器好用吗? 微信公众号图文素材 如何制作微信公众 ...
- 百度云搭建微信公众平台服务器,微信大众开放平台开发03-百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试...
微信公众开放平台开发03---百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试 微信公众开放平台开发03---百度 ...
- 添加管理微信公众号图片素材-微信公众号使用教程8
微信公众号发送消息给粉丝时, 有一种素材是经常用到的, 那就是图片. 公众号使用图片的方式 在公众号中使用图片有两种方式: 一种是直接复制粘贴, 另外一种是先把图片上传到微信公众号的素材库中, 在使用 ...
- 线上教育相关的微信公众号图文这样排版,阅读量翻十倍!
新冠肺炎疫情爆发后,线下课堂纷纷停课,各地的教育机构纷纷延迟开学,本以为没有老师的监管,能够轻松学习的时候,线上远程教育出现了, "钉钉"也因此评分大幅下降.因为上网可需要用到这个 ...
- sae微信公众平台php,SAE 上使用PHP搭建微信公众号后台
SAE 上使用PHP搭建微信公众号后台 准备阶段 SAE准备 SAE的应用平台提供了一个语言环境.比如提供了PHP环境的应用即可运行PHP代码.当然环境中也可以放HTML和CSS,将要展示的页面命名为 ...
- 微信公众平台开发之在网页上添加分享到朋友圈,关注微信号等按钮
微信公众平台开始支持前端网页,大家可能看到很多网页上都有分享到朋友圈,关注微信等按钮,点击它们都会弹出一个窗口让你分享和关注,这个是怎么实现的呢?今天就给大家讲解下如何在微信公众平台前端网页上添加分享 ...
最新文章
- 微信小程序下拉刷新和上拉加载的实现
- python3+selenium入门08-鼠标事件
- python---webRTC~vad静音检测-学习笔记
- PostgreSQL+安装及常见问题
- OpenCV3学习(2.4)——彩色图像读取、灰度图转化、RGB通道分割与合并
- python 闭包和装饰器
- 利用ICallbackEventHandler接口实现Ajax效果
- 1001	Hello,World!
- vue动态改变css样式
- StackPanel与Grid交叉使用
- 源码编译 Qt 6.2
- 码农和程序员之间的5个关键差异
- k8s nginx ingress 显示证书错误
- Qt中使用TCP和MC协议与三菱Q系列PLC通信
- 如何用python编写财务记账软件_Python实现简单的记账本功能
- 我的世界服务器java启动脚本_教程/服务器启动脚本
- hud 6078 Wavel Sequence
- iQOO5和iQOO5pro有什么区别
- arduino知识点梳理(二)——INPUT_PULLUP模式
- 一年外包经验入职字节啦
热门文章
- 那些著名的黑客事件 六
- 详解 CatBoost 原理
- 广东省计算机一级网络题分值,计算机一级考试的试题分值如何分配的?
- 大道至简---软件工程实践者的思想--------------第二章读后感---是懒人造就了方法...
- VBA IE对象的操作方法
- vue 百度地图 3d地图
- b区计算机211学校排名,考研B区院校排名
- 简单的C语言实训代码
- 关于“马太效应”,“蝴蝶效应”、“鲶鱼效应”的解释(转贴)
- 计算机软件系统故障的分类,系统故障