Merge branch 'develop' of gitee.com:darlk/ShengTangManage into develop

This commit is contained in:
huangdeliang
2021-02-03 17:28:57 +08:00
5 changed files with 394 additions and 289 deletions

View File

@ -1,7 +1,6 @@
package com.stdiet.custom.utils;
import com.alibaba.fastjson.JSONObject;
import com.stdiet.common.core.redis.RedisCache;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.common.utils.http.HttpUtils;
import com.stdiet.custom.domain.WxXmlData;
@ -9,28 +8,30 @@ import com.stdiet.custom.domain.wechat.WxAccessToken;
import com.stdiet.custom.domain.wechat.WxFileUploadResult;
import com.thoughtworks.xstream.XStream;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class WxTokenUtils {
public static final String KEY_ACCESS_TOKEN="wx:access_token";
public static final String KEY_ACCESS_TOKEN_WATHER="wx:access_token_watcher";
public static final String KEY_ACCESS_TOKEN = "wx:access_token";
public static final String KEY_ACCESS_TOKEN_WATHER = "wx:access_token_watcher";
// 与接口配置信息中的Token要一致
private static String token = "shengtangdiet";
// private static String appId = "wx4a9c1fc9dba53202";
// private static String appSecret = "fff029ade5d3575df755f4cf9e52f8da";
private static String appId = "wxaf10fe560ea043a0";
private static String appSecret = "afb47e477337df23b7562c3c1f965826";
// 胜唐体控
private static String appId = "wx4a9c1fc9dba53202";
private static String appSecret = "fff029ade5d3575df755f4cf9e52f8da";
// 胜唐体控李晓
// private static String appId = "wxaf10fe560ea043a0";
// private static String appSecret = "afb47e477337df23b7562c3c1f965826";
private static String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
private static String uploadMaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
private static String uploadMaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=image";
public static WxAccessToken fetchAccessToken() {
@ -48,18 +49,28 @@ public class WxTokenUtils {
}
}
public static WxFileUploadResult uploadImage(String filePath, String accessToken) {
try {
String url = uploadMaterialUrl + "?access_token" + accessToken + "&type=image";
HttpPostUtil post = new HttpPostUtil(url);
post.addParameter("media", new File(filePath));
String resultStr = post.send();
JSONObject obj = JSONObject.parseObject(resultStr);
WxFileUploadResult result = JSONObject.toJavaObject(obj, WxFileUploadResult.class);
return result;
} catch (Exception e) {
return null;
/**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中然后用输入流来获取url的响应
*
* @return String url的响应信息返回值
* @throws IOException
*/
public static WxFileUploadResult uploadImage(String filePath, String fileName, String accessToken) throws Exception {
String[] cmds = {"curl", uploadMaterialUrl.replaceAll("ACCESS_TOKEN", accessToken), "-F"
, "media=@" + filePath + ";filename=" + fileName};//必须分开写,不能有空格
ProcessBuilder process = new ProcessBuilder(cmds);
Process p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
JSONObject obj = JSONObject.parseObject(builder.toString());
return JSONObject.toJavaObject(obj, WxFileUploadResult.class);
}
/**