Public number upload temporary footage get Media_id__java

Source: Internet
Author: User
Tags stringbuffer
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; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.