java 微信里保存图片

@PostMapping(value = "getJsApiTicket")
    @ApiOperation("获取图片下载签名信息")
    @ResponseBody
    public ResultModel getJsApiTicket() throws UnsupportedEncodingException {
        String appid = props.getStr("appId");
        String secret = props.getStr("appSecret");
        Map accessToken = weixinutils.getAccessToken(appid, secret);
        String  accessTokens = accessToken.get("access_token");
        Map jsApiTicket = weixinutils.getJsApiTicket(accessTokens);
        String jsapi_ticket = jsApiTicket.get("ticket");
        Map sign = WXShareUtil.sign(jsapi_ticket, "http://dcdev5.natapp1.cc/x'x'x/index.html");//不支持Ip
        return ResultModel.success(sign);
    }
import cn.hutool.setting.dialect.Props;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public  class WXShareUtil {
	private static Props props = new Props("config.properties", "utf-8");
	private static String appId = props.getStr("appId");

	public static Map sign(String jsapi_ticket,String url) {
		Map ret = new HashMap();
		String nonce_str = create_nonce_str();
			String timestamp = create_timestamp();
			String string1;
			String signature = "";
			// 注意这里参数名必须全部小写,且必须有序
			string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str
					+ "×tamp=" + timestamp + "&url=" + url;
			//System.out.println(string1+"==================================");
			try {
				MessageDigest crypt = MessageDigest.getInstance("SHA-1");
				crypt.reset();
			crypt.update(string1.getBytes("UTF-8"));
			signature = byteToHex(crypt.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		ret.put("url", url);
		ret.put("jsapi_ticket", jsapi_ticket);
		ret.put("nonceStr", nonce_str);
		ret.put("timestamp", timestamp);
		ret.put("signature", signature);
		ret.put("appid",appId);
		return ret;
	}

	private static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}

	private static String create_nonce_str() {
		return UUID.randomUUID().toString();
	}

	private static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}
}
​
import java.util.HashMap;
import java.util.Map;
import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;


public  class  weixinutils {
    /**
     * @auther: Zww
     * 功能描述: 
     * @param: 获取全局的accessToken
     * @return:
     * @date: 2019/5/28 15:58
     */
    public static Map getAccessToken(String wx_appid, String wx_appsecret) {
        Map data = new HashMap();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ wx_appid + "&secret=" + wx_appsecret;
        JsonObject accessTokenInfo = null;
        try {
            Gson token_gson = new Gson();
            String result = HttpUtil.get(url);
            accessTokenInfo = token_gson.fromJson(result, JsonObject.class);
            data.put("access_token", accessTokenInfo.get("access_token").toString().replaceAll("\"", ""));
            data.put("expires_in", accessTokenInfo.get("expires_in").toString().replaceAll("\"", ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return data;
    }


    /**
     * @auther: Zww
     * 功能描述:
     * @param: 获取授权AccessToken
     * @return:
     * @date: 2019/5/28 15:39
     */
    public static Map getUserInfoAccessToken(String code,String appid,String AppSecret) {
        JsonObject object = null;
        Map data = new HashMap<>();
        String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appid, AppSecret, code);
        String result = HttpUtil.get(url);
        Gson token_gson = new Gson();
        object = token_gson.fromJson(result, JsonObject.class);
        data.put("openid", object.get("openid").toString().replaceAll("\"", ""));
        data.put("access_token", object.get("access_token").toString().replaceAll("\"", ""));
        return data;
    }


/**网页授权获取用户信息
 * 获取用户信息
 *
 * @param accessToken  网页授权获取的 token
 * @param openId
 * @return
 */
    public static Map getAutoUserInfo(String accessToken, String openId) {
        Map data = new HashMap();
        String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";
        JsonObject userInfo = null;
        try {
            String result = HttpUtil.get(url);
            Gson token_gson = new Gson();
            userInfo = token_gson.fromJson(result, JsonObject.class);
            data.put("openid", userInfo.get("openid").toString().replaceAll("\"", ""));
            data.put("sex", userInfo.get("sex").toString().replaceAll("\"", ""));
            data.put("nickname", userInfo.get("nickname").toString().replaceAll("\"", ""));
            data.put("city", userInfo.get("city").toString().replaceAll("\"", ""));
            data.put("province", userInfo.get("province").toString().replaceAll("\"", ""));
            data.put("country", userInfo.get("country").toString().replaceAll("\"", ""));
            data.put("headimgurl", userInfo.get("headimgurl").toString().replaceAll("\"", ""));
        } catch (Exception ex) {
        	ex.printStackTrace();
        }
        return data;
    }

   


    /**
     * 获取jsapi_ticket
     *
     * @return
     */
    public static Map getJsApiTicket(String accessToken) {
        Map data = new HashMap();
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi";
        JsonObject jsApiTicket = null;
        try {
            String result = HttpUtil.get(url);
            Gson token_gson = new Gson();
            jsApiTicket = token_gson.fromJson(result, JsonObject.class);
            data.put("ticket", jsApiTicket.get("ticket").toString().replaceAll("\"", ""));
            data.put("expires_in", jsApiTicket.get("expires_in").toString().replaceAll("\"", ""));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return data;
    }
    


}

​

 

你可能感兴趣的:(java 微信里保存图片)