微信小程序> 调用微信小程序API生成二维码并转base64存数据库-小程序码转二维码-小程序码在线生成

调用微信小程序API生成二维码并转base64存数据库-小程序码转二维码-小程序码在线生成

浏览量:3423 时间: 来源:尕舞丶
publicclassGeneralTokenUtil{privatestaticfinalStringappid"";privatestaticfinalStringgrant_type"";//获取token值publicstaticStringgetToken(StringcmpId){Stringline"";Stringaccess_token"";Stringqr"";try{Stringstr"https://api.weixin.qq.com/cgi-bin/token?appid"+appid+"grant_type"+grant_type;URLurlnewURL(str);//得到connection对象。HttpURLConnectionconnection(HttpURLConnection)url.openConnection();//设置请求方式connection.setRequestMethod("GET");//连接connection.connect();//得到响应码intresponseCodeconnection.getResponseCode();if(responseCodeHttpURLConnection.HTTP_OK){//得到响应流InputStreaminputStreamconnection.getInputStream();//获取响应BufferedReaderreadernewBufferedReader(newInputStreamReader(inputStream));while((linereader.readLine())!null){access_tokenline;System.out.println(line);}JSONObjectjsonObjectJSONObject.fromObject(access_token);StringnamejsonObject.getString("access_token");MapmapnewHashMap();map.put("scene",cmpId);map.put("page","pages/welcome/welcome");StringdateJSONObject.fromObject(map).toString();//转化成jsonStringstrToken"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token"+name;//qrgetQR(name,cmpId);qrhttpPostWithJSON(strToken,date,cmpId);reader.close();//该干的都干完了,记得把连接断了connection.disconnect();}}catch(Exceptione){e.printStackTrace();}returnqr;}publicstaticStringhttpPostWithJSON(Stringurl,Stringjson,Stringid)throwsException{Stringresultnull;HttpClienthttpClientHttpClientBuilder.create().build();HttpPosthttpPostnewHttpPost(url);httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");StringEntitysenewStringEntity(json);se.setContentType("application/json");se.setContentEncoding(newBasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));httpPost.setEntity(se);//httpClient.execute(httpPost);HttpResponseresponsehttpClient.execute(httpPost);if(response!null){HttpEntityresEntityresponse.getEntity();//if(resEntity!null){InputStreaminstreamsresEntity.getContent();//System.out.println(instreams);//StringuploadSysUrl"E://erweima/";//FilesaveFilenewFile(uploadSysUrl+id+".png");////判断这个文件(saveFile)是否存在//if(!saveFile.getParentFile().exists()){////如果不存在就创建这个文件夹//saveFile.getParentFile().mkdirs();//}//resultsaveToImgByInputStream(instreams,uploadSysUrl,id+".png");resultgetBase64FromInputStream(instreams);result"data:image/png;base64,"+result;//}}httpPost.abort();returnresult;}/**@paraminstreams二进制流**@paramimgPath图片的保存路径**@paramimgName图片的名称**@return1:保存正常0:保存失败*/publicstaticStringsaveToImgByInputStream(InputStreaminstreams,StringimgPath,StringimgName){Stringpath"";intstateInt1;if(instreams!null){try{FilefilenewFile(imgPath+imgName);//可以是任何图片格式.jpg,.png等FileOutputStreamfosnewFileOutputStream(file);byte[]bnewbyte[1024];intnRead0;while((nReadinstreams.read(b))!-1){fos.write(b,0,nRead);}fos.flush();fos.close();}catch(Exceptione){stateInt0;e.printStackTrace();}finally{try{instreams.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}returnpath;}/****转base64***/publicstaticStringgetBase64FromInputStream(InputStreamin){//将图片文件转化为字节数组字符串,并对其进行Base64编码处理byte[]datanull;//读取图片字节数组try{ByteArrayOutputStreamswapStreamnewByteArrayOutputStream();byte[]buffnewbyte[100];intrc0;while((rcin.read(buff,0,100))0){swapStream.write(buff,0,rc);}dataswapStream.toByteArray();}catch(IOExceptione){e.printStackTrace();}finally{if(in!null){try{in.close();}catch(IOExceptione){e.printStackTrace();}}}returnnewString(Base64.encodeBase64(data));}publicstaticvoidmain(String[]args){StringtokengetToken("1");System.out.println(token);}}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎