小言_互联网的博客

java获取微信小程序码乱码解决

440人阅读  评论(0)

看了几篇博客没get到点,后来自己尝试了一波,别用字符串接收,用输入流转字节,在转base64传给前端,前端那边就可以正常显示了
下面放代码

	@Override
    public String getItemDetailWxaqrcode(GetWxaqrcodeQuery getWxaqrcodeQuery) {
        // accessToken
        String getAccessToken = String.format(WechatInfo.GET_ACCESS_TOKEN, WechatInfo.APPID, WechatInfo.APPSECRET);
        String accessTokenResultStr = HttpClientUtil.get(getAccessToken);
        String accessToken = JSON.parseObject(accessTokenResultStr, JSONObject.class).getString("access_token");
        // 二维码
        String url = String.format(WechatInfo.GET_WXACODEUNLIMIT, accessToken);
       
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        //创建httpClient连接对象
        httpClient = HttpClients.createDefault();
        //创建post请求连接对象
        HttpPost httpPost = new HttpPost(url);
        //创建连接请求对象,并设置连接参数
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(15000)   //连接服务区主机超时时间
                .setConnectionRequestTimeout(60000) //连接请求超时时间
                .setSocketTimeout(60000).build(); //设置读取响应数据超时时间
        //为httppost请求设置参数
        httpPost.setConfig(requestConfig);
        //将上传参数放到entity属性中
        httpPost.setEntity(new StringEntity(JSON.toJSONString(getWxaqrcodeQuery), "UTF-8"));
        //添加头信息
        httpPost.addHeader("Content-type", "text/xml");
        String result = "";
        try {
            //发送请求
            httpResponse = httpClient.execute(httpPost);
            //从相应对象中获取返回内容
            HttpEntity entity = httpResponse.getEntity();
            InputStream content = entity.getContent();
            byte[] bytes = InputStreamToByte(content);
            result = Base64.encodeBase64String(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 输入流转字节流
     * */
    private byte[] InputStreamToByte(InputStream in) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int ch;
        while ((ch = in.read(buffer)) != -1) {
            bytestream.write(buffer,0,ch);
        }
        byte data[] = bytestream.toByteArray();
        bytestream.close();
        return data;
    }

引用的包:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qyb.emkshop.client.query.miniprogram.GetWxaqrcodeQuery;
import com.qyb.emkshop.client.service.miniprogram.MiniprogramCommonService;
import com.qyb.emkshop.core.constant.WechatInfo;
import com.qyb.emkshop.core.util.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

用的httpclient


转载:https://blog.csdn.net/weixin_43820012/article/details/104638731
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场