先创建几个实体类:
public class Image {
private String MediaId;
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
}
public class ImageMessage extends BaseMessage{
private Image Image;
public com.naruto.huo.model.Image getImage() {
return Image;
}
public void setImage(com.naruto.huo.model.Image image) {
Image = image;
}
}
public class BaseMessage {
private String ToUserName;
private String FromUserName;
private long CreateTime;
private String MsgType;
private long MsgId;
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public long getCreateTime() {
return CreateTime;
}
public void setCreateTime(long createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
public long getMsgId() {
return MsgId;
}
public void setMsgId(long msgId) {
MsgId = msgId;
}
}
1.创建common文件夹,里面放WeixinUtil:
public class WeixinUtil {
private static Logger log = LoggerFactory.getLogger(WeixinUtil.class);
private static final String APPID = "****";
private static final String APPSECRET="****";
public final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?"
+ "grant_type=client_credential&appid=APPID&secret=APPSECRET";
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log.error("Weixin server connection timed out.");
} catch (Exception e) {
log.error("https request error:{}", e);
}
return jsonObject;
}
public static AccessToken getAccessToken(String appid, String appsecret) {
AccessToken accessToken = null;
String requestUrl = ACCESS_TOKEN_URL.replace("APPID", appid).replace("APPSECRET", appsecret);
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
if (null != jsonObject) {
accessToken = new AccessToken();
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
}
return accessToken;
}
public static JSONObject doGetstr(String url){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity!=null){
String result = EntityUtils.toString(entity);
jsonObject = JSONObject.fromObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject doPoststr(String url,String outStr){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr, "utf-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(),"utf-8");
jsonObject =JSONObject.fromObject(result);
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static AccessToken getAccessToken(HttpSession session){
System.out.println("从接口中获取");
AccessToken token = new AccessToken();
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
JSONObject json = doGetstr(url);
System.out.println("存储的json:"+json.toString());
if(json!=null){
token.setToken(json.getString("access_token"));
token.setExpiresIn(json.getInt("expires_in"));
session.setAttribute("access_token",json.getString("access_token"));
System.out.println("存储的access_token:"+json.getString("access_token"));
}
return token;
}
public static String getAccess_Token(HttpSession session,HttpServletRequest request){
System.out.println("从缓存中读取");
Object access_token = request.getSession().getAttribute("access_token");
System.out.println("SESSION里的access_token是否获取到:"+access_token);
if(access_token==null){
AccessToken token = getAccessToken(session);
access_token = token.getToken();
}
return access_token.toString();
}
}
2.创建一个uploadUtil:
package com.naruto.huo.common
import net.sf.json.JSONObject
import java.io.*
import java.net.HttpURLConnection
import java.net.URL
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import java.security.NoSuchProviderException
public class UploadUtil {
private static final String UPLOAD_URL ="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"
/**
* 文件上传
* @param filePath
* @param accessToken
* @param type
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws KeyManagementException
*/
public static String upload(String filePath, String accessToken,String type) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
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
StringBuilder sb = new StringBuilder()
sb.append("--")
sb.append(BOUNDARY)
sb.append("\r\n")
sb.append("Content-Disposition: form-data
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
try {
//定义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()
}
} catch (IOException e) {
e.printStackTrace()
} finally {
if (reader != null) {
reader.close()
}
}
JSONObject jsonObj = JSONObject.fromObject(result)
String typeName = "media_id"
if("thumb".equals(type)){
typeName = type + "_media_id"
}
String mediaId = jsonObj.getString(typeName)
return mediaId
}
}
3.在之前(一)的controller加一个图片判断:
@Component
public class MessageDispatcher {
public static String processMessage(Map<String, String> map, HttpSession session, HttpServletRequest request) throws FileNotFoundException {
String openid = map.get("FromUserName");
String mpid = map.get("ToUserName");
String content = map.get("Content");
if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
TextMessage txtmsg = new TextMessage();
txtmsg.setToUserName(openid);
txtmsg.setFromUserName(mpid);
txtmsg.setCreateTime(new Date().getTime());
txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
if("接收".equals(content)){
txtmsg.setContent("接收到了!没错~");
}else if("图片".equals(content)){
MessageUtil util = new MessageUtil();
return util.initMessage(openid, mpid,session,request);
}else{
txtmsg.setContent("您输入的是:"+content);
}
return MessageUtil.textMessageToXml(txtmsg);
}
return null;
}
public String processEvent(Map<String, String> map) {
return "123";
}
}
4.在(一)的messageUtil里面加代码:
/*处理图片*/
/**
* 将信息转为xml格式
*/
public String messageToxml(ImageMessage imageMessage) {
XStream xtream = new XStream()
xtream.alias("xml", imageMessage.getClass())
xtream.alias("Image", new Image().getClass())
System.out.println("xtream.toString():"+xtream.toString())
return xtream.toXML(imageMessage)
}
/**
* 封装信息
*/
public String initMessage(String FromUserName, String ToUserName,HttpSession session, HttpServletRequest request) throws FileNotFoundException {
Image image = new Image()
image.setMediaId(getmediaId(session,request))
log.info("MediaId:"+image.getMediaId())
ImageMessage message = new ImageMessage()
message.setFromUserName(ToUserName)
message.setToUserName(FromUserName)
message.setCreateTime(new Date().getTime())
message.setImage(image)
log.info("ToUserName:"+ToUserName)
log.info("FromUserName:"+FromUserName)
message.setMsgType("image")
log.info("message:"+message.toString())
return messageToxml(message)
}
@Value("${config.extPath}")
private String extPath
/**
* 获取Media
* @return
*/
public String getmediaId(HttpSession session, HttpServletRequest request) throws FileNotFoundException {
//获取classes目录绝对路径
//ResourceUtils.getURL("classpath:").getPath()
//String path = request.getSession().getServletContext().getRealPath("/static/pic/feng.png")
//String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/images/feng.png"
String path = request.getServletContext().getRealPath("/") + "WEB-INF/classes/static/images/feng.png"
log.info("path:"+path)
String accessToken = WeixinUtil.getAccess_Token(session,request)
String mediaId = null
try {
mediaId = UploadUtil.upload(path, accessToken.toString(), "image")
} catch (KeyManagementException | NoSuchAlgorithmException
| NoSuchProviderException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return mediaId
}
/*处理结束*/
5.最后成功之后则为
