上传文件
This commit is contained in:
@ -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,7 +8,6 @@ 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.InputStream;
|
||||
@ -19,8 +17,8 @@ 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";
|
||||
@ -32,7 +30,6 @@ public class WxTokenUtils {
|
||||
private static String uploadMaterialUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material";
|
||||
|
||||
|
||||
|
||||
public static WxAccessToken fetchAccessToken() {
|
||||
try {
|
||||
String resStr = HttpUtils.sendGet(tokenUrl, "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret);
|
||||
@ -48,11 +45,12 @@ public class WxTokenUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static WxFileUploadResult uploadImage(String filePath, String accessToken) {
|
||||
public static WxFileUploadResult uploadImage(String filePath, String fileName, String accessToken) {
|
||||
try {
|
||||
String url = uploadMaterialUrl + "?access_token" + accessToken + "&type=image";
|
||||
HttpPostUtil post = new HttpPostUtil(url);
|
||||
post.addParameter("media", new File(filePath));
|
||||
post.addParameter("name", fileName);
|
||||
String resultStr = post.send();
|
||||
JSONObject obj = JSONObject.parseObject(resultStr);
|
||||
WxFileUploadResult result = JSONObject.toJavaObject(obj, WxFileUploadResult.class);
|
||||
|
Reference in New Issue
Block a user