WeChat calls the permission configuration of js interfaces such as photo taking and photo uploading and downloading, and js photo uploading

Source: Internet
Author: User
Tags date now

Permission configuration and photo upload and download implementation for calling js interfaces such as photo taking, js photo upload

Directly run the Code:

1. Front-end debugging code:

<Html> 

Front-end here

Var url = window. location. href. split ('#') [0];
It is easy to make mistakes.

2. backend interface:

@ NoLogin @ RequestMapping (value = "/getConfigInfo. json ", method = RequestMethod. POST) @ ResponseBody public Object getConfigInfo (String url) throws NoSuchAlgorithmException {String noncestr = "dsfww"; JsapiTicket ticket = AccessTokenJsapiTicketThread. getTicket (); logger. debug ("ticket ::::::;" + JSON. toJSONString (ticket); System. out. println ("ticket :::::;" + JSON. toJSONString (ticket); if (ticket! = Null) {long timestamp = new Date (). getTime (); StringBuilder sb = new StringBuilder ("jsapi_ticket ="); sb. append (ticket. getTicket ()). append ("& noncestr = "). append (noncestr ). append ("& timestamp = "). append (timestamp ). append ("& url = "). append (url); MessageDigest md = MessageDigest. getInstance ("SHA-1"); // encrypted string sha1 byte [] digest = md. digest (sb. toString (). getBytes (); String signature = SignUtil. byteToSt R (digest ). toLowerCase (); Map <String, String> map = new HashMap <String, String> (); map. put ("jsapi_ticket", ticket. getTicket (); map. put ("appId", WxPayConfPub. APPID); map. put ("timestamp", String. valueOf (timestamp); map. put ("nonceStr", noncestr); map. put ("signature", signature); map. put ("originalStr", sb. toString (); logger. debug (JSON. toJSONString (map); System. out. println (JSON. toJSONString (map); r Eturn JsonConvertor. convertSuccessResult (map);} return JsonConvertor. convertFailResult (ErrorCodeEnum. SYSTEM_ERROR) ;}@ NoLogin @ RequestMapping (value = "/getPhoto. json ", method = RequestMethod. POST) @ ResponseBody public Object getPhoto (String media_id) throws NoSuchAlgorithmException {// http Request Method: GET, https call // var url = "https://api.weixin.qq.com/cgi-bin/media/get? Access_token = ACCESS_TOKEN & media_id = MEDIA_ID "; AccessToken token = AccessTokenJsapiTicketThread. accessToken; String url =" https://api.weixin.qq.com/cgi-bin/media/get? Access_token = "+ token. getAccess_token () + "& media_id =" + media_id; HttpsURLConnection httpsUrl = null; InputStream inputStream = null; Date now = new Date (); String saveFileName = null; try {httpsUrl = HttpUtil. initHttpsConnection (url, "GET"); int responseCode = httpsUrl. getResponseCode (); if (responseCode = 200) {// an input stream inputStream = httpsUrl is returned from the server. getInputStream (); byte [] data = new byte [10 24]; int len = 0; FileOutputStream fileOutputStream = null; saveFileName = DateUtil. convertYMDH (now) + RandomStringUtils. random (6, true, true) + ". jpg "; // absolute path String path = imageRootPath + DateUtil. convertYMD (now) + "/" + saveFileName; File dir = new File (imageRootPath + DateUtil. convertYMD (now) + "/"); if (! Dir. exists () {FileUtils. forceMkdir (dir);} try {fileOutputStream = new FileOutputStream (path); while (len = inputStream. read (data ))! =-1) {fileOutputStream. write (data, 0, len);} fileOutputStream. flush ();} catch (IOException e) {e. printStackTrace ();} finally {if (inputStream! = Null) {try {inputStream. close () ;}catch (IOException e) {}} if (fileOutputStream! = Null) {try {fileOutputStream. close () ;}catch (IOException e) {}}} catch (IOException e) {e. printStackTrace ();} // return the image path return JsonConvertor. convertSuccessResult (DateUtil. convertYMD (now) + "/" + saveFileName );}

/GetConfigInfo. json interface is used to configure jsapi permission authentication.

/GetPhoto. json downloads the photo from the server and saves it to our own server. Then, return the part of our own server to the front-end for use.

  

 

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.