package com.tuanzi.utils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by IntelliJ IDEA.
*
* @author 团子
* description:
* path: springboot-push-com.tuanzi.utils-RedisUtils
* date: 2019/7/4 16:57
*/
public class RedisUtils {
private final static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
/**
* 获取access_token
*/
private final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
/**
*推送url
**/
private final static String PUSH_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";
private final static String APP_ID = "AppID(小程序ID)";
private final static String SECRET = "AppSecret(小程序密钥)";
/**
* 获取access_token
* @author 团子
* @return
*/
public static JSONObject getAccessToken() {
String url = ACCESS_TOKEN_URL + "appid=" + APP_ID + "&secret=" + SECRET;
PrintWriter out = null;
BufferedReader in = null;
String line;
StringBuffer sb = new StringBuffer();
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性 设置请求格式
//设置返回类型
conn.setRequestProperty("contentType", "text/plain");
//设置请求类型
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//设置超时时间
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);
conn.setDoOutput(true);
conn.connect();
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应 设置接收格式
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = in.readLine()) != null) {
sb.append(line);
}
// 将获得的String对象转为JSON格式
JSONObject jsonObject = JSONObject.parseObject(sb.toString());
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* 推送工具类
* @author 团子
* @param params 推送消息内容
* @param accessToken
* @return boolean
*/
public static boolean setPush(String params, String accessToken) {
boolean flag = false;
String url = PUSH_URL + accessToken;
OutputStream outputStream = null;
InputStreamReader inputStreamReader = null;
InputStream inputStream = null;
BufferedReader bufferedReader = null;
HttpsURLConnection connection = null;
try {
// 创建URL对象
URL realUrl = new URL(url);
// 打开连接 获取连接对象
connection = (HttpsURLConnection) realUrl.openConnection();
// 设置请求编码
connection.addRequestProperty("encoding", "UTF-8");
// 设置允许输入
connection.setDoInput(true);
// 设置允许输出
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != params) {
outputStream = connection.getOutputStream();
// 注意编码格式
outputStream.write(params.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
inputStream = connection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, "utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
JSONObject jsonObject = JSONObject.parseObject(buffer.toString());
int errorCode = jsonObject.getInteger("errcode");
String errorMessage = jsonObject.getString("errmsg");
if (errorCode == 0) {
flag = true;
} else {
logger.info("模板消息发送失败:" + errorCode + "," + errorMessage);
flag = false;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 依次关闭打开的输入流
try {
connection.disconnect();
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
// 依次关闭打开的输出流
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot实现微信模板消息推送
共82个文件
xml:63个
java:5个
class:4个
1星 需积分: 46 63 下载量 56 浏览量
2019-07-04
17:35:04
上传
评论 2
收藏 124KB ZIP 举报
温馨提示
springboot实现微信模板消息推送,包含微信端代码。简单封装,方便二次开发!
资源推荐
资源详情
资源评论
收起资源包目录
微信模板消息推送.zip (82个子文件)
springboot-push
springboot-push.iml 8KB
mvnw.cmd 6KB
HELP.md 532B
mvnw 9KB
src
test
java
com
tuanzi
SpringbootPushApplicationTests.java 344B
main
resources
templates
static
application.yml 20B
java
com
tuanzi
utils
RedisUtils.java 6KB
SpringbootPushApplication.java 331B
controller
PushController.java 2KB
target
generated-test-sources
test-annotations
generated-sources
annotations
classes
com
tuanzi
utils
RedisUtils.class 5KB
controller
PushController.class 2KB
SpringbootPushApplication.class 721B
application.yml 20B
test-classes
com
tuanzi
SpringbootPushApplicationTests.class 640B
.idea
libraries
Maven__org_springframework_spring_jcl_5_1_8_RELEASE.xml 681B
Maven__org_projectlombok_lombok_1_18_8.xml 596B
Maven__net_bytebuddy_byte_buddy_agent_1_9_13.xml 650B
Maven__com_fasterxml_classmate_1_4_0.xml 594B
Maven__ch_qos_logback_logback_core_1_2_3.xml 619B
Maven__org_hibernate_validator_hibernate_validator_6_0_17_Final.xml 753B
Maven__junit_junit_4_12.xml 527B
Maven__com_fasterxml_jackson_core_jackson_databind_2_9_9.xml 695B
Maven__com_jayway_jsonpath_json_path_2_4_0.xml 618B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_21.xml 732B
Maven__javax_annotation_javax_annotation_api_1_3_2.xml 683B
Maven__ch_qos_logback_logback_classic_1_2_3.xml 640B
Maven__org_springframework_spring_context_5_1_8_RELEASE.xml 709B
Maven__org_apache_logging_log4j_log4j_api_2_11_2.xml 645B
Maven__org_springframework_boot_spring_boot_starter_2_1_6_RELEASE.xml 764B
Maven__org_springframework_boot_spring_boot_test_2_1_6_RELEASE.xml 743B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_21.xml 697B
Maven__org_springframework_boot_spring_boot_starter_json_2_1_6_RELEASE.xml 799B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_1_6_RELEASE.xml 813B
Maven__org_springframework_spring_expression_5_1_8_RELEASE.xml 730B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_11_2.xml 680B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_9_9.xml 760B
Maven__org_xmlunit_xmlunit_core_2_6_2.xml 607B
Maven__net_bytebuddy_byte_buddy_1_9_13.xml 608B
Maven__org_apache_tomcat_embed_tomcat_embed_el_9_0_21.xml 683B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 772B
Maven__org_hamcrest_hamcrest_core_1_3.xml 604B
Maven__net_minidev_accessors_smart_1_2.xml 614B
Maven__org_springframework_spring_core_5_1_8_RELEASE.xml 688B
Maven__org_ow2_asm_asm_5_0_4.xml 544B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml 716B
Maven__com_alibaba_fastjson_1_2_49.xml 586B
Maven__org_springframework_boot_spring_boot_starter_test_2_1_6_RELEASE.xml 799B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_9_9.xml 746B
Maven__org_springframework_spring_webmvc_5_1_8_RELEASE.xml 702B
Maven__org_springframework_spring_test_5_1_8_RELEASE.xml 688B
Maven__org_hamcrest_hamcrest_library_1_3.xml 625B
Maven__com_fasterxml_jackson_core_jackson_core_2_9_9.xml 667B
Maven__org_springframework_boot_spring_boot_2_1_6_RELEASE.xml 708B
Maven__org_mockito_mockito_core_2_23_4.xml 614B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_9_9.xml 801B
Maven__org_slf4j_slf4j_api_1_7_26.xml 585B
Maven__org_springframework_spring_aop_5_1_8_RELEASE.xml 681B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_1_6_RELEASE.xml 841B
Maven__org_jboss_logging_jboss_logging_3_3_2_Final.xml 680B
Maven__org_springframework_spring_beans_5_1_8_RELEASE.xml 695B
Maven__org_objenesis_objenesis_2_6.xml 580B
Maven__org_skyscreamer_jsonassert_1_5_0.xml 609B
Maven__org_springframework_boot_spring_boot_starter_logging_2_1_6_RELEASE.xml 820B
Maven__javax_validation_validation_api_2_0_1_Final.xml 683B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_1_6_RELEASE.xml 806B
Maven__org_springframework_spring_web_5_1_8_RELEASE.xml 681B
Maven__org_springframework_boot_spring_boot_starter_web_2_1_6_RELEASE.xml 792B
Maven__org_assertj_assertj_core_3_11_1.xml 614B
Maven__net_minidev_json_smart_2_3.xml 579B
Maven__org_yaml_snakeyaml_1_23.xml 567B
Maven__org_slf4j_jul_to_slf4j_1_7_26.xml 606B
misc.xml 451B
compiler.xml 657B
workspace.xml 17KB
encodings.xml 177B
modules.xml 277B
.mvn
wrapper
MavenWrapperDownloader.java 5KB
maven-wrapper.jar 47KB
maven-wrapper.properties 116B
pom.xml 2KB
.gitignore 333B
push.zip 23KB
共 82 条
- 1
资源评论
- 风清扬逍遥子2019-09-19不能用,不要下载,坑人,不写启动流程麻烦你不要随便骗分!!!团子.2019-09-20代码是好的,可惜啊,人不行!不行我给你写个用户手册?????
团子.
- 粉丝: 137
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功