java微信公众号上传永久素材_微信开放平台永久素材视频文件上传
对于微信开放平台接口的API写的很详细,但是我们在开发中还是会遇到一些问题。
出现这些问题可能是我们的经验不足也可能是知识面教窄。
微信永久素材的上传分为图片,音频,视频还有图文。在做音频文件和图片上传的时候很顺利。
上代码☀
// 拼装请求地址
String uploadMediaUrl = "http://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + this.getAccessTokenFromCache();
URL url = new URL(uploadMediaUrl);
String result = null;
// String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
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("--").append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"type\" \r\n\r\n");
sb.append(type.getValue()).append("\r\n");
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"media\";filename=\"")
.append(fileName).append("\";filelength=\"").append(filelength).append("\" \r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
OutputStream out = new DataOutputStream(con.getOutputStream());
out.write(head);
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = inputStream.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
inputStream.close();
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
out.write(foot);
out.flush();
out.close();
StringBuilder buffer = new StringBuilder();
BufferedReader reader = null;
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(
con.getInputStream(), "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
// 使用JSONNode解析返回结果
JsonNode ifjson = objectMapper.readTree(result);
//直接返回获取到的数据,在页面上进行将返回值赋值
return ifjson.toString();
这些很顺利就完成了。
但是当我在做视频素材上传的时候,发现需要传一个参数,参数内容包裹在一个json当中,参数名是description,这个时候我就懵了/(ㄒoㄒ)/~~ ,这可怎么传啊 。
然后我就试一下在浏览器中直接把视频发送给微信。上传成功。
话说我为什么要这么做呢~~ 其实就想看这个参数在浏览器中是如何发送的。发现这个参数根文件一样 也是划分了一个BOUNDARY 只是多加了一个块。这样参数就传过去了。上面的代码修改后。
// 拼装请求地址
String uploadMediaUrl = "http://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + this.getAccessTokenFromCache();
URL url = new URL(uploadMediaUrl);
String result = null;
// String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
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();
//视频文件 加一个块 块里面是description 这个参数
if (type.equals(MaterialType.VIDEO)) {
JSONObject json = new JSONObject();
json.put("title", title);
json.put("introduction", introduction);
sb.append("--").append(BOUNDARY); // 必须多两道线 http 协议头
sb.append("\r\n");
sb.append("Content-Disposition: form-data; name=\"description\"");
sb.append("\r\n");
sb.append("\r\n");
sb.append(json.toString());
}
sb.append("--").append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"type\" \r\n\r\n");
sb.append(type.getValue()).append("\r\n");
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
//这里是media参数相关的信息
sb.append("Content-Disposition: form-data;name=\"media\";filename=\"")
.append(fileName).append("\";filelength=\"").append(filelength).append("\" \r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
out.write(head);
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = inputStream.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
inputStream.close();
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
out.write(foot);
out.flush();
out.close();
StringBuilder buffer = new StringBuilder();
BufferedReader reader = null;
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(
con.getInputStream(), "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
// 使用JSONNode解析返回结果
JsonNode ifjson = objectMapper.readTree(result);
//直接返回获取到的数据,在页面上进行将返回值赋值
return ifjson.toString();
完毕。
更多推荐
所有评论(0)