Mooner_guo 2015-07-17 08:49 采纳率: 0%
浏览 3858

一般微信公众平台素材管理是怎么做的?

总感觉微信素材管理上传素材的的接口不太给力,还得实用curl,在web中,使用
HttpURLConnection上传图片可以,语音和视频总是上传不成功,求解答!!

 RL url = new URL(uploadMediaUrl);
            String result = null;
            File file = new File(mediaFileUrl);
            if (!file.exists() || !file.isFile()) {
                System.out.println("上传的文件不存在");
            }
            String filename = file.getName();  
            HttpURLConnection con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false); // post方式不能使用缓存

            // 设置请求头信息
            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("--"); // 必须多两道线
            sb.append(BOUNDARY);
            sb.append("\r\n");
/***********************************/
            sb.append("Content-Disposition: form-data;name=\"type\" \r\n\r\n");
            sb.append(type);
            sb.append("\r\n--"); // 必须多两道线
            sb.append(BOUNDARY);
            sb.append("\r\n");
           if("video".equals(type)){
               sb.append("Content-Disposition: form-data;name=\"description\" \r\n\r\n");
               sb.append("{\"title\":\"hello title\",\"introduction\":\"hello introduction\"}");
               sb.append("\r\n--"); // 必须多两道线
               sb.append(BOUNDARY);
               sb.append("\r\n");
            }
/***********************************/
            sb.append("Content-Disposition: form-data;type=\""+type+"\";name=\"media\";filename=\""
                            + filename + "\" \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");
            // 获得输出流
            out = new DataOutputStream(con.getOutputStream());
            // 输出表头
            out.write(head);
            // 文件正文部分
            // 把文件已流文件的方式 推入到url中
            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[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
            out.write(foot);
            out.flush();
            out.close();
            StringBuffer buffer = new StringBuffer();
            // 定义BufferedReader输入流来读取URL的响应
            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                    buffer.append(line);
            }
            if (result == null) {
                result = buffer.toString();
            }
  • 写回答

2条回答

  • threenewbee 2015-07-17 11:05
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊