上传语音素材返回 errcode":41005,"errmsg":"media data missing hint:"

qq_40067751 2018-04-08 10:51:17
@Override
public JSONObject uploadVoiceToWx(Long userid, MultipartFile file, HttpServletRequest request) throws Exception {
System.out.println("yiyi");
JSONObject dataMsg = new JSONObject();
if (file.isEmpty()) {
dataMsg.put("error", "-1");
dataMsg.put("msg", "上传文件不能为空");
dataMsg.put("data", "");
return dataMsg;
}
String wxid = request.getParameter("wxid");
try {
if (StringUtils.isEmpty(wxid)) {
dataMsg.put("error", -1);
dataMsg.put("msg", "不能缺少wxid哦!");
dataMsg.put("data", "");
return dataMsg;
}
//获取token
// JSONObject findToken = restTemplate.getForObject("http://jmz-weixin-wxcore/token/" + wxid,
// JSONObject.class);
// System.out.println("findToken:" + findToken.toJSONString());
// String token = findToken.getString("data");
// System.out.println("token:" + token);

// 拼装请求地址
String uploadMediaUrl = "http://api.weixin.qq.com/cgi-bin/material/add_material?access_token=##ACCESS_TOKEN##";
uploadMediaUrl = uploadMediaUrl.replace("##ACCESS_TOKEN##",
"8_euUrbavESOwoTlarswvezZRCxsrjqha1ka0ATHohdC1CrN79oog00Mr8c8JtjKNPFQXTUYJWjWsABIyrFyBKtv91Y4d4b4WgnT61KBo4DDhzSNoHzHMClOXTThtbqgeUbAz-sJ03QyFiUR7nZADdABALMP");

URL url = new URL(uploadMediaUrl);
long filelength = file.getSize();
String type="voice";

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false); // post方式不能使用缓存
// 设置请求头信息
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");

// 设置边界,这里的boundary是http协议里面的分割符,不懂的可惜百度(http 协议 boundary),这里boundary 可以是任意的值(111,2222)都行
String BOUNDARY = "----------" + System.currentTimeMillis();
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
// 请求正文信息
// 第一部分:

StringBuilder sb = new StringBuilder();
System.out.println(sb);

//这块是post提交type的值也就是文件对应的mime类型值
sb.append("--"); // 必须多两道线 这里说明下,这两个横杠是http协议要求的,用来分隔提交的参数用的,不懂的可以看看http 协议头
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"type\" \r\n\r\n"); //这里是参数名,参数名和值之间要用两次
sb.append(type+"\r\n"); //参数的值

sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"media\";filename=\""
+ file.getOriginalFilename() + "\";filelength=\"" + filelength + "\" \r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
//System.out.println(sb.toString());
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 输出表头
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
//
//
//
// 将CommonsMultipartFile转出file
// CommonsMultipartFile cf= (CommonsMultipartFile)file;
// DiskFileItem fi = (DiskFileItem)cf.getFileItem();
// File f = fi.getStoreLocation();

StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
conn.disconnect();
JSONObject jsontemp = JSONObject.parseObject(buffer.toString());
log.info("jsontemp" + jsontemp);
// 音频上传成功,返回media_id
if (jsontemp.containsKey("media_id")) {
dataMsg.put("error", 0);
dataMsg.put("msg", "上传音频成功!");
dataMsg.put("media_id", jsontemp.getString("media_id"));
return dataMsg;
} else {
dataMsg.put("error", -1);
dataMsg.put("msg", jsontemp.getString("errmsg"));
dataMsg.put("data", "");
return dataMsg;
}
} catch (Exception e) {
log.error("发送POST请求出现异常!" + e.toString());
//throw new Exception(e);
return null;
}

}
...全文
2103 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_37225682 2019-09-19
  • 打赏
  • 举报
回复
这么简单 都不会
uk8692 2018-04-09
  • 打赏
  • 举报
回复
你看下是否少传了什么参数,或者是参数传递的方式不对,看你写的好像有问题,怎么form-data;name=\"type\",之后又写了一个form-data;name=\"media\";
qq_40067751 2018-04-08
  • 打赏
  • 举报
回复
求救啊

3,156

社区成员

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

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