java 微信公众号 图文消息推送

package com.example.swaggerDemo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.swaggerDemo.entity.ArticleEntity;
import com.example.swaggerDemo.entity.SysUserEntity;
import com.example.swaggerDemo.service.ArticleService;
import com.example.swaggerDemo.service.UserService;
import com.example.swaggerDemo.util.MyHttpUtils;

import com.example.swaggerDemo.wxpush.xstream;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.nio.charset.Charset;

import java.sql.SQLException;

import java.util.*;


@Api("微信图文推送")
@RestController
@RequestMapping("/articlewx")
public class ArticlewxController {
    @Autowired
    private ArticleService Service;

    @Autowired
    private UserService SysUserDao;

    /**
     * 获取token
     * @param appId
     * @param appSecret
     * @return
     */
    public static String getAccessToken(String appId, String appSecret) {
        StringBuffer userInfoUrl =
                new StringBuffer("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
                        .append("&appid=").append(appId)
                        .append("&secret=").append(appSecret);
        String userInfoStr = MyHttpUtils.doPost(userInfoUrl.toString(), "", Charset.forName("UTF-8").toString());
        Map<String, String> map = null;

        return userInfoStr;
    }

    @ApiOperation(value="",notes="")
    @ResponseBody
    @RequestMapping(value="/findwx",method= RequestMethod.POST)
    public  void SendQYMessageimg(@RequestParam("id") String id)
    {
          String Url="http://****/view/addetails.html?id="+id;
          String mp = getAccessToken("wxd4aff866a1642b28","3baceeec4066458afe6850bb689ec9c6");
          JSONObject rq = JSON.parseObject(mp);
         String accessToken=  rq.get("access_token").toString();
         String strurl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
          List<SysUserEntity> dto  = null;
          try {
              ArticleEntity art = Service.findid(id);
              dto = SysUserDao.findall();
;
              String Title=art.getTitle();
              String Description=art.getPretext();
              String PicUrl="http://*****"+ art.getPreimage();
          for (SysUserEntity q: dto)
            {
                String responeJsonStr = "{";
                responeJsonStr += "\"touser\": \"" + q.getOpenid() + "\",";
                responeJsonStr += "\"msgtype\": \"news\",";
                responeJsonStr += "\"agentid\": \"wxd4aff8****\",";
                responeJsonStr += "\"news\": {";
                responeJsonStr += "\"articles\": [{";
                responeJsonStr += "  \"title\": \"" + Title + "\",";
                responeJsonStr += "  \"description\": \" " + Description + "\",";
                responeJsonStr += "  \"url\": \"" + Url + "\",";
                responeJsonStr += "  \"picurl\": \"" + PicUrl + "\"";
                responeJsonStr += "}]";
                responeJsonStr += "},";
                responeJsonStr += "\"safe\":\"0\"";
                responeJsonStr += "}";
                xstream.connectWeiXinInterface(strurl,responeJsonStr);
            }
          } catch (SQLException e) {
              e.printStackTrace();
          }

    }

}
-------------------------------------------------------------------------------------------
package com.example.swaggerDemo.wxpush;

import java.io.InputStream;
import java.io.OutputStream;

import java.net.HttpURLConnection;
import java.net.URL;

public class xstream {

    public static void connectWeiXinInterface(String action,String json){

        URL url;

        try {

            url = new URL(action);

            HttpURLConnection http = (HttpURLConnection) url.openConnection();

            http.setRequestMethod("POST");

            http.setRequestProperty("Content-Type",

                    "application/x-www-form-urlencoded");

            http.setDoOutput(true);

            http.setDoInput(true);

            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒

            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒

            http.connect();

            OutputStream os = http.getOutputStream();

            os.write(json.getBytes("UTF-8"));// 传入参数

            InputStream is = http.getInputStream();

            int size = is.available();

            byte[] jsonBytes = new byte[size];

            is.read(jsonBytes);

            String result = new String(jsonBytes, "UTF-8");

            System.out.println("请求返回结果:"+result);

            os.flush();

            os.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }



}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值