String imgName = UUID.randomUUID().toString().replace("-", "") +".png";
String QRPath = this.getServletContext().getRealPath("/pay/QRIMG/wx") + imgName;
使用上述的方式可以解决路径问题;在项目中直接引用该文件就可以了
2、在多个项目中,由于微信的授权登陆只能配置一个,如下图(不同域名进行授权解决方案):
![](https://i-blog.csdnimg.cn/blog_migrate/2ee806763b3866f10fb8a49612c854db.png)
![](https://i-blog.csdnimg.cn/blog_migrate/409143fcaf556f46e53f2fffedde134d.png)
此时我们想获得到微信授权到不同的域名,此时的方法为:
![](https://i-blog.csdnimg.cn/blog_migrate/c06b3d2a4e16bcfa22491713fcdac935.png)
这里贴出2个主要的授权方法:
![](https://i-blog.csdnimg.cn/blog_migrate/32e3b32b92c20b19bcced0b64238f43f.jpeg)
package com.huawei.nser.wap.wxtransformation;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.huawei.nser.pub.SJBUtil;
import com.huawei.nser.wap.wxtransformation.model.TransfmMessageBean;
import com.huawei.nser.wap.wxtransformation.service.TransfmMessageService;
/**
*
* @author 获取微信id并跳转
*
*/
public class WapTransWXAction extends Action{
private Logger log = LoggerFactory.getLogger(WapTransWXAction.class);
private TransfmMessageService transfmMessageService ;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
log.info("@@@@@ WapTransWXAction.execute enter this method");
transfmMessageService = (TransfmMessageService) SJBUtil.getBean("transfmMessage");
//获取信息并保持内容
String id = UUID.randomUUID().toString().replace("-", "");
String field1 = request.getParameter("field1");
String field2 = request.getParameter("field2");
String field3 = request.getParameter("field3");
String call_back_url = request.getParameter("callBackUrl");
TransfmMessageBean transfmMessageBean = new TransfmMessageBean();
transfmMessageBean.setId(id);
transfmMessageBean.setField1(field1);
transfmMessageBean.setField2(field2);
transfmMessageBean.setField3(field3);
transfmMessageBean.setCall_back_url(call_back_url);
//step1 保存转换信息
transfmMessageService.saveTransFmMessage(transfmMessageBean);
String authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4b19bad466392998&redirect_uri=http://babyhhcsy.imwork.net/wapnew/ngwap/weixinopenid.do?id="+id+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
log.info("@@@@@ WapTransWXAction.execute get authorizeurl is :{}",authorizeUrl);
//step 3 调取微信;
response.sendRedirect(authorizeUrl);
return null;
}
public Map<String, String> request2Map(HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String key = names.nextElement();
String value = request.getParameter(key);
if (value == null || value.trim().equals("")) {
continue;
}
map.put(key, value);
}
return map;
}
public String map2String(Map<String,String> map){
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append(entry.getKey()+"="+entry.getValue()+";");
}
Log.info(sb.toString());
return sb.toString();
}
public TransfmMessageService getTransfmMessageService() {
return transfmMessageService;
}
public void setTransfmMessageService(TransfmMessageService transfmMessageService) {
this.transfmMessageService = transfmMessageService;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6fab7b0a93c939f409026ebbd41dc57f.jpeg)
package com.huawei.nser.wap.wxtransformation;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.huawei.nser.pub.SJBUtil;
import com.huawei.nser.wap.bankPay.util.HttpsRequestUtil;
import com.huawei.nser.wap.wxtransformation.model.TransfmMessageBean;
import com.huawei.nser.wap.wxtransformation.service.TransfmMessageService;
/**
* 微信转换类,获得code内容
* @author thero
*
*/
public class WXTransAction extends Action {
private Logger log = LoggerFactory.getLogger(WapTransWXAction.class);
private static final String code2openId = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=myappid&secret=mysecret&code=mycode&grant_type=authorization_code";
private static final long serialVersionUID = 1L;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String code = request.getParameter("code");
log.info("AuthorizeUserServlet.doPost get code is :{},get paramter is {}",code,request.getQueryString());
//step 1 获得id ;
String id = request.getParameter("id");
log.info("@@@@@ get url from wx is :{},and id is",request.getRequestURL(),id);
//step 2 从数据库查询id并获得url;
TransfmMessageService transfmMessageService = (TransfmMessageService) SJBUtil.getBean("transfmMessage");
TransfmMessageBean dbtransfmMessage = transfmMessageService.querytransfmMessage(id);
if(dbtransfmMessage!=null){
String call_back_url = dbtransfmMessage.getCall_back_url();
String openid = this.code2OpenId(request, code, response);
dbtransfmMessage.setOpenid(openid);
transfmMessageService.saveTransFmMessage2(dbtransfmMessage);
StringBuffer sb = new StringBuffer();
sb.append(call_back_url);
sb.append("?openid="+openid);
if(null!=dbtransfmMessage.getField1()&& !"".equals(dbtransfmMessage.getField1())){
sb.append("&fields1="+dbtransfmMessage.getField1());
}
if(null!=dbtransfmMessage.getField2()&& !"".equals(dbtransfmMessage.getField2())){
sb.append("&fields2="+dbtransfmMessage.getField2());
}
if(null!=dbtransfmMessage.getField3()&& !"".equals(dbtransfmMessage.getField3())){
sb.append("&fields3="+dbtransfmMessage.getField3());
}
response.sendRedirect(sb.toString());
return null;
}
return null;
}
public String code2OpenId(HttpServletRequest request,String code, HttpServletResponse response) throws ServletException, IOException{
String openId = null;
String tempUrl = code2openId.replace("myappid", "wxXXXXX8").replace("mysecret", "13XXXXXXc66").replace("mycode",code);
//String tempUrl = code2openId.replace("myappid", "wXXXXXf").replace("mysecret", "7aXXXXXc28").replace("mycode",code);
log.info("AuthorizeUserServlet.code2OpenId get tempUrl is :{}",tempUrl);
JSONObject result = HttpsRequestUtil.httpsRequest(tempUrl, HttpsRequestUtil.POST,"");
log.info("AuthorizeUserServlet.code2OpenId get openid is :{}",result.toString());
if(!(null!=result && result.get("openid")!=null)){
return null;
}else{
return result.get("openid").toString();
}
}
public Map<String, String> request2Map(HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String key = names.nextElement();
String value = request.getParameter(key);
if (value == null || value.trim().equals("")) {
continue;
}
map.put(key, value);
}
return map;
}
public String map2String(Map<String, String> map) {
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, String> entry : map.entrySet()) {
sb.append(entry.getKey() + "=" + entry.getValue() + ";");
}
log.info(sb.toString());
return sb.toString();
}
}
数据库脚本:
prompt PL/SQL Developer import file
prompt Created on 2015年7月17日 星期五 by wangjirong
set feedback off
set define off
prompt Creating EC_GET_OPEN_ID...
create table EC_GET_OPEN_ID
(
id VARCHAR2(32) not null,
call_back_url VARCHAR2(300),
openid VARCHAR2(28),
session_id VARCHAR2(24),
field1 VARCHAR2(30),
field2 VARCHAR2(30),
field3 VARCHAR2(30)
)
tablespace ECARE
pctfree 10
initrans 20
maxtrans 255
storage
(
initial 1M
next 2M
minextents 1
maxextents unlimited
);
comment on table EC_GET_OPEN_ID
is '微信统一获得openid数据库';
comment on column EC_GET_OPEN_ID.id
is 'id主键';
comment on column EC_GET_OPEN_ID.call_back_url
is '回调url地址';
comment on column EC_GET_OPEN_ID.openid
is 'openid微信';
comment on column EC_GET_OPEN_ID.session_id
is '请求session位置';
comment on column EC_GET_OPEN_ID.field1
is '备用字段1';
comment on column EC_GET_OPEN_ID.field2
is '备用字段2';
comment on column EC_GET_OPEN_ID.field3
is '备用字段3';
alter table EC_GET_OPEN_ID
add constraint PK primary key (ID)
using index
tablespace ECARE
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
prompt Disabling triggers for EC_GET_OPEN_ID...
alter table EC_GET_OPEN_ID disable all triggers;
prompt Loading EC_GET_OPEN_ID...
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('121', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', 'oznhbuPT3DkbBHmZCWbU4Gr4cbjw', null, '123', '321', null);
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('feb0aaed1cda4c868b569bb2c6202a68', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', null, null, '123', '321', null);
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('078ed17fb5f14665af5c5abef8570bf9', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', null, null, '123', '321', null);
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('020e9039d75d4ea88b9a8e79e8310bc5', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', null, null, '123', '321', null);
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('65a063d899e44349a906d652228d7759', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', null, null, '123', '321', null);
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('cb9e40134a3043319af8ef70fff208af', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', null, null, '123', '321', null);
insert into EC_GET_OPEN_ID (id, call_back_url, openid, session_id, field1, field2, field3)
values ('a789ee91f5d749a89a20fe102f93dddb', 'http://192.168.88.210:8071/weixin/pay/pay.jsp', null, null, '123', '321', null);
commit;
prompt 7 records loaded
prompt Enabling triggers for EC_GET_OPEN_ID...
alter table EC_GET_OPEN_ID enable all triggers;
set feedback on
set define on
prompt Done.
3、微信openid的区别:
在开发微信的时候,如果你使用了测试账户,测试账户值得是:在公众平台设置的测试账号;测试你用过授权得到的openid,和实际使用的openid是不一致的!,需要特别的注意,在正式上线的时候,需要使用真实的openid;
使用测试账号的openid和测试的首选域,是可以完成支付测试的,需要注意:只
![](https://i-blog.csdnimg.cn/blog_migrate/a8ad4b30248e0a68b44dabb8a8fb2246.png)
公众平台设置了测试授权目录才能进行相应的支付测试,负责,不能完成支付;