public number upload temporary footage get media_id
Java language
Update to 2018-01-15
public class util{private static final String Upload_url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_to
Ken=access_token&type=type "; public static string UploadFile (String FilePath, String Accesstoken, String type) throws exception{file File = NE
W File (FilePath); if (!file.exists () | | |!file.isfile ()) {throw new IOException ("file does not exist.")
");
String url = upload_url.replace ("Access_token", Accesstoken). Replace ("type", type);
URL urlobj = new URL (URL);
Connection HttpURLConnection conn = (httpurlconnection) urlobj.openconnection ();
Conn.setrequestmethod ("POST");
Conn.setdoinput (TRUE);
Conn.setdooutput (TRUE);
Conn.setusecaches (FALSE);
Request Head Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("Charset", "UTF-8");
Conn.setrequestproperty ("Content-type", "multipart/form-data;"); Set Boundary String boundary = "----------" + system.currenttimemillis ();
Conn.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=\" file\ "filename=\" "+file.getname () +" \ "\ r \ n");
Sb.append ("content-type:application/octet-stream\r\n\r\n");
Byte[] head = sb.tostring (). GetBytes ("Utf-8");
Output stream OutputStream out = new DataOutputStream (Conn.getoutputstream ());
Out.write (head);
File body part 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 ();
End byte[] Foot = ("\r\n--" + boundary + "--\r\n"). GetBytes ("Utf-8"); Out.write (foot);
Out.flush ();
Out.close ();
Get response StringBuffer buffer = new StringBuffer ();
BufferedReader reader = null;
String result = null;
reader = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));
String line = null;
while (line = Reader.readline ())!= null) {buffer.append (line);
} if (result = = null) {result = Buffer.tostring ();
} reader.close ();
Need to add Json-lib jar bundle Jsonobject JSON = jsonobject.fromobject (result);
SYSTEM.OUT.PRINTLN (JSON);
String mediaId = json.getstring ("media_id");
return mediaId; }
}