微信开发,求新增永久素材接口的java调用示例
新增临时素材接口可以调通,但是新增永久素材接口就是调不通。实在走投无路了。
我是用这个方法调用的
public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) { String res = ""; HttpURLConnection conn = null; String BOUNDARY = "---------------------------"+ System.currentTimeMillis(); //boundary就是request头和上传文件内容的分隔符 try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(20000); conn.setReadTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); // text if (textMap != null) { StringBuffer strBuf = new StringBuffer(); Iterator iter = textMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String inputName = (String) entry.getKey(); String inputValue = (String) entry.getValue(); if (inputValue == null) { continue; } strBuf.append("rn").append("--").append(BOUNDARY).append( "rn"); strBuf.append("Content-Disposition: form-data; name="" + inputName + ""rnrn"); strBuf.append(inputValue); } out.write(strBuf.toString().getBytes("utf-8")); } // file if (fileMap != null) { Iterator iter = fileMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String inputName = (String) entry.getKey(); String inputValue = (String) entry.getValue(); if (inputValue == null) { continue; } File file = new File(inputValue); String filename = file.getName(); String contentType = new MimetypesFileTypeMap() .getContentType(file); if (filename.endsWith(".png")) { contentType = "image/png"; }else if(filename.endsWith(".jpg")){ contentType = "image/jpeg"; }else if(filename.endsWith(".mp4")){ contentType = "video/mpeg4"; } if (contentType == null || contentType.equals("")) { contentType = "application/octet-stream"; } StringBuffer strBuf = new StringBuffer(); strBuf.append("rn").append("--").append(BOUNDARY).append("rn"); strBuf.append("Content-Disposition: form-data; name="" + inputName + ""; filename="" + filename + ""rn"); strBuf.append("Content-Type:" + contentType + "rnrn"); out.write(strBuf.toString().getBytes("utf-8")); DataInputStream in = new DataInputStream( new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); } } byte[] endData = ("rn--" + BOUNDARY + "--rn").getBytes("utf-8"); out.write(endData); out.flush(); out.close(); // 读取返回数据 StringBuffer strBuf = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { strBuf.append(line).append("n"); } res = strBuf.toString(); reader.close(); reader = null; } catch (Exception e) { System.out.println("发送POST请求出错。" + urlStr); e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); conn = null; } } System.out.println(res); JSONObject jsonObj = JSONObject.fromObject(res); String mediaId = jsonObj.getString("media_id"); return mediaId; }
看微信的接口文档我也是醉了,都是Php示例。这里面说要提交两个表单,到底怎么弄啊。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
把这个form-data; name=""+ inputName +"";改为form-data; name=""+media+"";
获取永久图片素材的接口有没有人调通,求指教
我后来直接在linux里使用curl命令,发现根本调不通,无解
微信小白路过,帮你顶下。
求教,同样遇到这个问题,上传永久素材,缺少多媒体文件
帮忙顶。请教下新增其他类型的永久素材时,
media参数要传些什么东西。
我去。。。沉了~
自己顶
这个方法上传永久的可以,临时的有问题
我找到办法了