uploadUrl String uploadUrl ="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"; uploadUrl = uploadUrl.replace("ACCESS_TOKEN", access_token).replace("TYPE", mediaType); try { // 创建一个https连接 URL requstUrl = new URL(uploadUrl); HttpsURLConnection conn = (HttpsURLConnection) requstUrl.openConnection(); conn.setRequestMethod("POST");//设置请求方式 conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); //设置请求消息头 conn.setRequestProperty("Connection","Keep-Alive"); conn.setRequestProperty("Charset","UTF-8"); String boundary ="----" + System.currentTimeMillis();//分隔字符串 conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary); //拼接请求消息体的格式头 StringBuilder sb1 = new StringBuilder(); sb1.append("--"); sb1.append(boundary); sb1.append("\r\n"); //回车 sb1.append("Content-Disposition:form-data;name=\"file\";filename=\""+mediaUrl+"\"\r\n");//回车 sb1.append("Content-Type:application/octet-stream\r\n");//回车 sb1.append("\r\n");//回车 - 空行 byte[] head = sb1.toString().getBytes("UTF-8"); //获得连接的写入流out DataOutputStreamout = new DataOutputStream(conn.getOutputStream()); //将请求消息体的格式头写入conn的写入流out中out.write(head); //读数据要用的 int len =0; byte[] buf = new byte[1024]; //>>>>>>>>>>