Authored by unknown

update

... ... @@ -13,4 +13,6 @@ public class MessageSenderConstant {
public static final String TEMP_FILE_PATH = "/tempFile/";
public static final String IMG_SUFFIX = ".png";
}
... ...
... ... @@ -249,6 +249,7 @@ public class WechatServiceImpl implements IWechatService {
//处理文案内容若为图片,则需要上传图片至微信素材中心,获取 media_id 拿来发消息 获取失败,直接返回
if(customMsgBO.getMsgType().equals(WechatCustomMsgTypeEnum.IMAGE.getText()) && !getMediaId(customMsgBO, accessToken)){
logger.warn("customMsgSend getMediaId error, with sceneKey is {}",customMsgBO.getSendKey());
return new ArrayList<>();
}
... ... @@ -274,24 +275,29 @@ public class WechatServiceImpl implements IWechatService {
private boolean getMediaId(WechatCustomMsgBO customMsgBO, AccessToken accessToken) {
try {
//下载网络图片
String fileName = String.valueOf(System.currentTimeMillis()) + ".png";
String fileName = String.valueOf(System.currentTimeMillis()) + MessageSenderConstant.IMG_SUFFIX;
String pathFile = MessageSenderConstant.TEMP_FILE_PATH + fileName;
HttpClientUtil.downFromPicUrl(customMsgBO.getSendContent(), MessageSenderConstant.TEMP_FILE_PATH, fileName);
//上传微信 获取 media_id
String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN","sdjfwoeijowen").replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
String addMediaUrl = getMediaUrl(accessToken);
//发送微信请求
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMediaUrl,new File(pathFile));
if(StringUtils.isEmpty(mediaIdResult)){
logger.warn("getMediaId with get mediaId null,imgUrl is {}",customMsgBO.getSendContent());
logger.warn("getMediaId with get result null,sceneKey is {},imgUrl is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
return false;
}
JSONObject resultObj = JSONObject.parseObject(mediaIdResult);
//校验返回结果
if (!checkResult(customMsgBO, resultObj,fileName)) return false;
//删除下载的图片
new File(MessageSenderConstant.TEMP_FILE_PATH).delete();
JSONObject object = new JSONObject();
deleteFile(pathFile);
JSONObject object = new JSONObject();
object.put("media_id",resultObj.get("media_id"));
customMsgBO.setSendContent(object.toJSONString());
logger.info("getMediaId success end,sceneKey is {},result is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | NoSuchProviderException e) {
logger.error("customMsgSend with IO post error,e is {}",e);
return false;
... ... @@ -299,6 +305,16 @@ public class WechatServiceImpl implements IWechatService {
return true;
}
private void deleteFile(String pathFile) {
if(!new File(pathFile).delete()){
logger.warn("delete file error,file is {}",pathFile);
}
}
private String getMediaUrl(AccessToken accessToken) {
return Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",accessToken.getToken()).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
}
private boolean checkResult(WechatCustomMsgBO customMsgBO, JSONObject resultObj,String fileName) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
if(resultObj.get("errcode") != null){
//这里重新获取token再调用一次
... ... @@ -306,6 +322,7 @@ public class WechatServiceImpl implements IWechatService {
String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",newAccessToken).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
if(StringUtils.isEmpty(mediaIdResult) || JSONObject.parseObject(mediaIdResult).get("errcode") != null){
logger.warn("checkResult with try again error,sceneKey is {},error is {}",customMsgBO.getSendKey(),mediaIdResult);
return false;
}
resultObj.put("media_id",JSONObject.parseObject(mediaIdResult).get("media_id"));
... ...
... ... @@ -227,14 +227,8 @@ public class HttpClientUtil {
// 请求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"media\";filelength=\"" + file.length() + "\";filename=\""
+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
byte[] head = ("--" + BOUNDARY + "\r\n" + "Content-Disposition: form-data;name=\"media\";filelength=\"" + file.length() + "\";filename=\""
+ file.getName() + "\"\r\n" + "Content-Type:application/octet-stream\r\n\r\n").getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
... ... @@ -242,7 +236,6 @@ public class HttpClientUtil {
// 文件正文部分
// 把文件已流文件的方式 推入到url中
// DataInputStream in = new DataInputStream(file);
InputStream inputStream = new FileInputStream(file);
int bytes;
byte[] bufferOut = new byte[1024];
... ... @@ -255,20 +248,18 @@ public class HttpClientUtil {
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
StringBuilder sbuilder = new StringBuilder();
BufferedReader reader = null;
try {
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
sbuilder.append(line);
}
result = sbuilder.toString();
} catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
System.out.println("httpPost error is " + e);
e.printStackTrace();
} finally {
if (reader != null) {
... ...