微信公众号平台如何使用java上传视频素材

xiaoming_dai 2015-05-12 04:48:11
下面是我上传视频素材的代码,但是返回的是{"errcode":-1,"errmsg":"system error"}
参数:
requestUrl = “http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ViuWPniaMkeerWvEO7X8GC8tNGCxeYs-jurzRonQqrzSLUHX1U4rR3yHsD8Ravr-70SQW0Xi1XhdehAzINECP8Il6d9z1ySt6pFn9qgjji4&type=video”
requestMethod = “POST”
mediaUrlStr = “http://localhost:8080/uploadFile/weChatInfoRelease/mp4/2015/5/12/153420435992.mp4”

public static JSONObject httpsRequestForUpload(String requestUrl,
String requestMethod, String mediaUrlStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url
.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
/** 设置请求方式(GET/POST) */
httpUrlConn.setRequestMethod(requestMethod);
httpUrlConn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=----" + boundary);
OutputStream outputStream = httpUrlConn.getOutputStream();
URL mediaUrl = new URL(mediaUrlStr);
HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl
.openConnection();
mediaConn.setReadTimeout(5000);
mediaConn.setDoOutput(true);
mediaConn.setRequestMethod(Constants.GET);
// 请求头中获取内容类型
String fileExt = mediaUrlStr
.substring(mediaUrlStr.lastIndexOf("."));
String contentType = getContentType(fileExt);
String fileName = mediaUrlStr.substring(
mediaUrlStr.lastIndexOf("/") + 1,
mediaUrlStr.lastIndexOf("."));
// 请求开始
outputStream.write(("------" + boundary + "\r\n").getBytes());
outputStream
.write(String
.format("Content-Disposition:form-data;name=\"media\";filename=\"%s%s\"\r\n",
fileName, fileExt).getBytes());
outputStream.write(String.format("Content-Type:%s\r\n\r\n",
contentType).getBytes());
// 获取媒体文件的输入流(读取文件)
BufferedInputStream bis = new BufferedInputStream(
mediaConn.getInputStream());
byte[] buf = new byte[8096];
int size = 0;
while ((size = bis.read(buf)) != -1) {
outputStream.write(buf, 0, size);
}
// 请求体结束
outputStream.write(("\r\n------" + boundary + "--\r\n").getBytes());
outputStream.close();
bis.close();
mediaConn.disconnect();

/** 将返回的输入流转换成字符串 */
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);

String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
/** 释放资源 */
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
logger.error("Weixin server connection timed out.");
} catch (Exception e) {
logger.error("https request error:" + e);
}
return jsonObject;
}

求高手指教
...全文
635 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
栀寒_信强 2018-12-24
  • 打赏
  • 举报
回复
问题解决,视频大小超出限制,上传视频不能超过10M
zeng_fy 2016-05-10
  • 打赏
  • 举报
回复
楼主,,我的代码跟你差不多,上传的是MP4格式,报40005,,不合法的文件类型,,求楼主指教

3,143

社区成员

发帖
与我相关
我的任务
社区描述
微信开发即微信公众平台开发,将企业信息、服务、活动等内容通过微信网页的方式进行表现,通过二次开发可以将公众账号由一个媒体型营销工具转化成提供服务的产品。
社区管理员
  • 微信开发
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧