package com.example.demo.Util;
import com.example.demo.Menu.Button;
import com.example.demo.Menu.ClickButton;
import com.example.demo.Menu.Menu;
import com.example.demo.Menu.ViewButton;
import com.example.demo.po.AccessToken;
import com.example.demo.trans.Data;
import com.example.demo.trans.Parts;
import com.example.demo.trans.Symbols;
import com.example.demo.trans.TransResult;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.collections.Buffer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2017/5/19.
*/
public class WeiXinUtil {
private static final String APPID = "wx813ca84e0d8e7dc8";
private static final String APPSECRET = "cdce24c6c51a7c4ead2d41924daeb621";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
private static final String QUERY_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
private static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
public static String upload(String filePath, String accessToken, String type) throws IOException {
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
String url = UPLOAD_URL.replace("ACCESS_TOKEN", accessToken).replace("TYPE", type);
URL urlObj = new URL(url);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
//设置边界
String BOUNDARY = "--------" + System.currentTimeMillis();
con.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");
//获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
//输出表头
out.write(head);
//文件正文部分
//把文件已流文件的方式,推入到url中
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();
//结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");//定义最后数据分割线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
String result = null;
//定义bufferedreader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
if (reader != null) {
reader.close();
}
JSONObject jsonObject = JSONObject.fromObject(result);
System.out.println(jsonObject);
String mediaId = jsonObject.getString("media_id");
return mediaId;
}
/**
* get请求
*
* @param url
* @return
*/
public static JSONObject doGetStr(String url) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String result = EntityUtils.toString(httpEntity, "UTF-8");
jsonObject = JSONObject.fromObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* post请求
*
* @param url
* @param outStr
* @return
*/
public static JSONObject doPostStr(String url, String outStr) throws IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
jsonObject = JSONObject.fromObject(result);
return jsonObject;
}
/**
* 获取AccessToken
*
* @return
*/
public static AccessToken getAccessToken() {
AccessToken token = new AccessToken();
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
JSONObject jsonObject = doGetStr(url);
if (jsonObject != null) {
token.setToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
}
return token;
}
/**
* 组装菜单
*
* @return
*/
public static Menu initMenu() {
Menu menu = new Menu();
ClickButton button11 = new ClickButton();
button11.setName("click菜单");
button11.setType("click");
button11.setKey("11");
ViewButton button21 = new ViewButton();
button21.setName("view菜单");
button21.setType("view");
button21.setUrl("http://www.baidu.com");
ClickButton button31 = new ClickButton();
button31.setName("扫码");
button31.setType("scancode_push");
button31.setKey("31");
ClickButton button32 = new ClickButton();
button32.setName("地理位置");
button32.setType("location_select");
button32.setKey("32");
Button button = new Button();
button.setName("菜单");
button.setSub_button(new Button[]{button32, button31});
menu.setButton(ne
没有合适的资源?快使用搜索试试~ 我知道了~
Java微信公众号开发的例子
共155个文件
xml:83个
class:25个
java:24个
3星 · 超过75%的资源 需积分: 9 207 下载量 170 浏览量
2017-05-23
10:15:33
上传
评论 3
收藏 6.47MB ZIP 举报
温馨提示
附有详细说明,和可用的ngrok,保证跟着说明配置一下就可以用了! 跟着慕课网老毕老师的视频敲出来的项目,只是老师那时用的百度翻译API过时了,我这里重新做好了。 我是用maven管理jar包的,所以怕有的人不会导包,我将我的pom.xml贴出来,要是那个包导错了,你们就看一下我怎么写的。
资源推荐
资源详情
资源评论
收起资源包目录
Java微信公众号开发的例子 (155个子文件)
WeiXinUtil.class 12KB
MessageUtil.class 8KB
WeixinController.class 4KB
MD5.class 3KB
CheckUtil.class 2KB
WeixinApplication.class 2KB
Music.class 1KB
BaseMessage.class 1KB
Symbols.class 1KB
TransResult.class 1KB
News.class 1KB
NewsMessage.class 1KB
Button.class 1KB
WeiXinUtil$1.class 974B
Data.class 852B
Parts.class 792B
TextMessage.class 775B
AccessToken.class 765B
WeixinApplicationTests.class 628B
ImageMessage.class 604B
MusicMessage.class 604B
Menu.class 576B
ClickButton.class 561B
ViewButton.class 558B
Image.class 549B
mvnw.cmd 5KB
说明.docx 193KB
ngrok.exe 15.41MB
.gitignore 249B
weixin.iml 9KB
httpclient-4.2.5.jar 423KB
commons-codec-1.6.jar 227KB
httpcore-4.2.4.jar 222KB
json-lib-2.4-jdk15.jar 155KB
httpclient-cache-4.2.5.jar 113KB
commons-logging-1.1.1.jar 59KB
maven-wrapper.jar 46KB
httpmime-4.2.5.jar 26KB
fluent-hc-4.2.5.jar 21KB
WeiXinUtil.java 13KB
MessageUtil.java 8KB
WeixinController.java 5KB
MD5.java 4KB
CheckUtil.java 1KB
WeixinApplication.java 1KB
BaseMessage.java 909B
Music.java 840B
News.java 808B
TextMessage.java 800B
Button.java 655B
Symbols.java 593B
TransResult.java 590B
NewsMessage.java 567B
AccessToken.java 480B
Data.java 372B
ImageMessage.java 341B
WeixinApplicationTests.java 333B
Parts.java 321B
Menu.java 295B
ClickButton.java 290B
Image.java 290B
ViewButton.java 288B
MusicMessage.java 206B
111.jpg 12KB
111.jpg 12KB
111.jpg 8KB
111.jpg 8KB
mvnw 6KB
.name 6B
maven-wrapper.properties 110B
application.properties 0B
application.properties 0B
workspace.xml 59KB
uiDesigner.xml 9KB
pom.xml 3KB
misc.xml 1KB
compiler.xml 1KB
Maven__org_springframework_boot_spring_boot_starter_data_mongodb_1_5_3_RELEASE.xml 783B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_1_5_3_RELEASE.xml 769B
Maven__org_springframework_boot_spring_boot_starter_thymeleaf_1_5_3_RELEASE.xml 762B
Maven__org_springframework_boot_spring_boot_starter_logging_1_5_3_RELEASE.xml 748B
Maven__org_springframework_boot_spring_boot_starter_tomcat_1_5_3_RELEASE.xml 741B
Maven__org_springframework_boot_spring_boot_autoconfigure_1_5_3_RELEASE.xml 734B
Maven__org_springframework_boot_spring_boot_starter_test_1_5_3_RELEASE.xml 727B
Maven__org_springframework_boot_spring_boot_starter_web_1_5_3_RELEASE.xml 720B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 700B
Maven__org_springframework_data_spring_data_commons_1_13_3_RELEASE.xml 699B
Maven__org_springframework_data_spring_data_mongodb_1_10_3_RELEASE.xml 699B
Maven__org_springframework_boot_spring_boot_starter_1_5_3_RELEASE.xml 692B
Maven__org_springframework_boot_spring_boot_test_1_5_3_RELEASE.xml 671B
Maven__nz_net_ultraq_thymeleaf_thymeleaf_layout_dialect_1_4_0.xml 667B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_8_5_14.xml 660B
Maven__org_springframework_spring_expression_4_3_8_RELEASE.xml 658B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_8_0.xml 644B
Maven__org_springframework_spring_context_4_3_8_RELEASE.xml 637B
Maven__org_springframework_boot_spring_boot_1_5_3_RELEASE.xml 636B
Maven__org_thymeleaf_thymeleaf_spring4_2_1_5_RELEASE.xml 634B
Maven__org_hibernate_hibernate_validator_5_3_5_Final.xml 634B
Maven__org_springframework_spring_webmvc_4_3_8_RELEASE.xml 630B
Maven__org_apache_tomcat_embed_tomcat_embed_core_8_5_14.xml 625B
共 155 条
- 1
- 2
资源评论
- 森林守护精灵2019-09-12不是我想要的资源
- sisi.li82018-05-28还不错的资源
- 崔东玉2017-12-20不会用IDEA呀,Eclipse可以导入么!明天你好丶2018-01-05可以 要有配置 springBoot 再用maven管理
- cihanfee2017-08-13不要被骗了,这个是PLSQL,根本不是什么微信开发的源代码明天你好丶2017-09-22PLSQL?你逗我呢,我这个项目完全没用到SQL,逗我呢 就是慕课网的跟着打出来的代码,别瞎几把评论
明天你好丶
- 粉丝: 6
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功