微信小程序,分享功能:微信小程序二维码的生成

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_45135322/article/details/90602685

首先是获取AccessToken,上一篇有获取方法,网上其他的也OK。

// 获取 小程序二维码
	@SuppressWarnings("unchecked")
	public static String getWXShare(String scene,String page,String RFID,HttpServletRequest request) throws Exception {
	
		//保存路径,示例  
		String uploadpath = ConfigUtil.get_instances().getuploadpath();/upload/imgs
		//全路径(带域名),https://sss.ss-ss.cn/upload/imgs/  本地写localhost.....
		String path = request.getServletContext().getRealPath(uploadpath);
		
		
		try {
			String accessToken = getAccessToken();//获取的AccessToken
			
			String redUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;

			JSONObject json = new JSONObject();
            json.put("scene", scene);		//需要携带的参数,不需要可以不写.   
            						 		//page 是扫码进去的路径,默认  page/index/index 。主页面的话,可不写
            
            String newFileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssms") +".jpg";//名字
            String fileName = "WX"+newFileName;
			String result = HttpURLConnectionUtil.httpPostWithJSON(redUrl, json.toJSONString(), path, fileName);
			if(result.equals("1")) {
				result = uploadpath+fileName;
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "获取分享码失败~!";
		}
	}

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpURLConnectionUtil {
    private static Logger log = LoggerFactory.getLogger(HttpURLConnection.class);
    private static final String APPLICATION_JSON="application/json";
    private static final String CONTENT_TYPE_TEXT_JSON="text/json";

    public static String httpPostWithJSON(String url,String json,String filePath,String fileName) throws IOException {
        String result = null;
//        将JSON进行UTF-8编码,以便传输中文
        String encodeJson = URLEncoder.encode(json,HTTP.UTF_8);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
        StringEntity se = new StringEntity(json);
        se.setContentType(CONTENT_TYPE_TEXT_JSON);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,APPLICATION_JSON));
        httpPost.setEntity(se);

        HttpResponse response = httpClient.execute(httpPost);
        if(response != null){
            HttpEntity resEntity = response.getEntity();
            if(resEntity != null){
                InputStream instreams = resEntity.getContent();
                int code = saveToImgByInputStream(instreams,fileName,filePath);
                if(code>0){
                    result = "1";
                }else{
                    result = "生成文件错误";
                }
            }
        }
        return result;
    }

    /**
     * 将二进制转换成文件保存
     * @param instreams
     * @param imgName
     * @param imgPath
     * @return
     *  1.保存正常
     *  0.保存失败
     */
    private static int saveToImgByInputStream(InputStream instreams, String imgName, String imgPath) {
        int stateInt = 1;
        if(instreams != null){
            try{
                File file = new File(imgPath,imgName);//可以是任何图片格式.jpg,png等
                FileOutputStream fos = new FileOutputStream(file);
                byte[] b = new byte[1024];
                int nRead = 0;
                while((nRead = instreams.read(b))!=-1){
                    fos.write(b,0,nRead);
                }
                fos.flush();
                fos.close();
                instreams.close();
            }catch (Exception e){
                stateInt = 0;
                log.error("读取图片出错",e);
            }finally {

            }
        }
        return stateInt;
    }

}

展开阅读全文

没有更多推荐了,返回首页