Authored by unknown

update

@@ -13,4 +13,6 @@ public class MessageSenderConstant { @@ -13,4 +13,6 @@ public class MessageSenderConstant {
13 13
14 public static final String TEMP_FILE_PATH = "/tempFile/"; 14 public static final String TEMP_FILE_PATH = "/tempFile/";
15 15
  16 + public static final String IMG_SUFFIX = ".png";
  17 +
16 } 18 }
@@ -249,6 +249,7 @@ public class WechatServiceImpl implements IWechatService { @@ -249,6 +249,7 @@ public class WechatServiceImpl implements IWechatService {
249 249
250 //处理文案内容若为图片,则需要上传图片至微信素材中心,获取 media_id 拿来发消息 获取失败,直接返回 250 //处理文案内容若为图片,则需要上传图片至微信素材中心,获取 media_id 拿来发消息 获取失败,直接返回
251 if(customMsgBO.getMsgType().equals(WechatCustomMsgTypeEnum.IMAGE.getText()) && !getMediaId(customMsgBO, accessToken)){ 251 if(customMsgBO.getMsgType().equals(WechatCustomMsgTypeEnum.IMAGE.getText()) && !getMediaId(customMsgBO, accessToken)){
  252 + logger.warn("customMsgSend getMediaId error, with sceneKey is {}",customMsgBO.getSendKey());
252 return new ArrayList<>(); 253 return new ArrayList<>();
253 } 254 }
254 255
@@ -274,24 +275,29 @@ public class WechatServiceImpl implements IWechatService { @@ -274,24 +275,29 @@ public class WechatServiceImpl implements IWechatService {
274 private boolean getMediaId(WechatCustomMsgBO customMsgBO, AccessToken accessToken) { 275 private boolean getMediaId(WechatCustomMsgBO customMsgBO, AccessToken accessToken) {
275 try { 276 try {
276 //下载网络图片 277 //下载网络图片
277 - String fileName = String.valueOf(System.currentTimeMillis()) + ".png"; 278 + String fileName = String.valueOf(System.currentTimeMillis()) + MessageSenderConstant.IMG_SUFFIX;
  279 + String pathFile = MessageSenderConstant.TEMP_FILE_PATH + fileName;
278 HttpClientUtil.downFromPicUrl(customMsgBO.getSendContent(), MessageSenderConstant.TEMP_FILE_PATH, fileName); 280 HttpClientUtil.downFromPicUrl(customMsgBO.getSendContent(), MessageSenderConstant.TEMP_FILE_PATH, fileName);
279 //上传微信 获取 media_id 281 //上传微信 获取 media_id
280 - String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN","sdjfwoeijowen").replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());  
281 - String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName)); 282 + String addMediaUrl = getMediaUrl(accessToken);
  283 + //发送微信请求
  284 + String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMediaUrl,new File(pathFile));
282 if(StringUtils.isEmpty(mediaIdResult)){ 285 if(StringUtils.isEmpty(mediaIdResult)){
283 - logger.warn("getMediaId with get mediaId null,imgUrl is {}",customMsgBO.getSendContent()); 286 + logger.warn("getMediaId with get result null,sceneKey is {},imgUrl is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
284 return false; 287 return false;
285 } 288 }
286 JSONObject resultObj = JSONObject.parseObject(mediaIdResult); 289 JSONObject resultObj = JSONObject.parseObject(mediaIdResult);
287 290
  291 + //校验返回结果
288 if (!checkResult(customMsgBO, resultObj,fileName)) return false; 292 if (!checkResult(customMsgBO, resultObj,fileName)) return false;
289 293
290 //删除下载的图片 294 //删除下载的图片
291 - new File(MessageSenderConstant.TEMP_FILE_PATH).delete(); 295 + deleteFile(pathFile);
  296 +
292 JSONObject object = new JSONObject(); 297 JSONObject object = new JSONObject();
293 object.put("media_id",resultObj.get("media_id")); 298 object.put("media_id",resultObj.get("media_id"));
294 customMsgBO.setSendContent(object.toJSONString()); 299 customMsgBO.setSendContent(object.toJSONString());
  300 + logger.info("getMediaId success end,sceneKey is {},result is {}",customMsgBO.getSendKey(),customMsgBO.getSendContent());
295 } catch (IOException | NoSuchAlgorithmException | KeyManagementException | NoSuchProviderException e) { 301 } catch (IOException | NoSuchAlgorithmException | KeyManagementException | NoSuchProviderException e) {
296 logger.error("customMsgSend with IO post error,e is {}",e); 302 logger.error("customMsgSend with IO post error,e is {}",e);
297 return false; 303 return false;
@@ -299,6 +305,16 @@ public class WechatServiceImpl implements IWechatService { @@ -299,6 +305,16 @@ public class WechatServiceImpl implements IWechatService {
299 return true; 305 return true;
300 } 306 }
301 307
  308 + private void deleteFile(String pathFile) {
  309 + if(!new File(pathFile).delete()){
  310 + logger.warn("delete file error,file is {}",pathFile);
  311 + }
  312 + }
  313 +
  314 + private String getMediaUrl(AccessToken accessToken) {
  315 + return Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",accessToken.getToken()).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
  316 + }
  317 +
302 private boolean checkResult(WechatCustomMsgBO customMsgBO, JSONObject resultObj,String fileName) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { 318 private boolean checkResult(WechatCustomMsgBO customMsgBO, JSONObject resultObj,String fileName) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
303 if(resultObj.get("errcode") != null){ 319 if(resultObj.get("errcode") != null){
304 //这里重新获取token再调用一次 320 //这里重新获取token再调用一次
@@ -306,6 +322,7 @@ public class WechatServiceImpl implements IWechatService { @@ -306,6 +322,7 @@ public class WechatServiceImpl implements IWechatService {
306 String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",newAccessToken).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText()); 322 String addMateralUrl = Consts.ADD_MATERIAL_URL.replace("ACCESSTOKEN",newAccessToken).replace("TYPE", WechatCustomMsgTypeEnum.IMAGE.getText());
307 String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName)); 323 String mediaIdResult = HttpClientUtil.connectHttpsByPost(addMateralUrl,new File(MessageSenderConstant.TEMP_FILE_PATH + fileName));
308 if(StringUtils.isEmpty(mediaIdResult) || JSONObject.parseObject(mediaIdResult).get("errcode") != null){ 324 if(StringUtils.isEmpty(mediaIdResult) || JSONObject.parseObject(mediaIdResult).get("errcode") != null){
  325 + logger.warn("checkResult with try again error,sceneKey is {},error is {}",customMsgBO.getSendKey(),mediaIdResult);
309 return false; 326 return false;
310 } 327 }
311 resultObj.put("media_id",JSONObject.parseObject(mediaIdResult).get("media_id")); 328 resultObj.put("media_id",JSONObject.parseObject(mediaIdResult).get("media_id"));
@@ -227,14 +227,8 @@ public class HttpClientUtil { @@ -227,14 +227,8 @@ public class HttpClientUtil {
227 227
228 // 请求正文信息 228 // 请求正文信息
229 // 第一部分: 229 // 第一部分:
230 - StringBuilder sb = new StringBuilder();  
231 - sb.append("--"); // 必须多两道线  
232 - sb.append(BOUNDARY);  
233 - sb.append("\r\n");  
234 - sb.append("Content-Disposition: form-data;name=\"media\";filelength=\"" + file.length() + "\";filename=\""  
235 - + file.getName() + "\"\r\n");  
236 - sb.append("Content-Type:application/octet-stream\r\n\r\n");  
237 - byte[] head = sb.toString().getBytes("utf-8"); 230 + byte[] head = ("--" + BOUNDARY + "\r\n" + "Content-Disposition: form-data;name=\"media\";filelength=\"" + file.length() + "\";filename=\""
  231 + + file.getName() + "\"\r\n" + "Content-Type:application/octet-stream\r\n\r\n").getBytes("utf-8");
238 // 获得输出流 232 // 获得输出流
239 OutputStream out = new DataOutputStream(con.getOutputStream()); 233 OutputStream out = new DataOutputStream(con.getOutputStream());
240 // 输出表头 234 // 输出表头
@@ -242,7 +236,6 @@ public class HttpClientUtil { @@ -242,7 +236,6 @@ public class HttpClientUtil {
242 236
243 // 文件正文部分 237 // 文件正文部分
244 // 把文件已流文件的方式 推入到url中 238 // 把文件已流文件的方式 推入到url中
245 - // DataInputStream in = new DataInputStream(file);  
246 InputStream inputStream = new FileInputStream(file); 239 InputStream inputStream = new FileInputStream(file);
247 int bytes; 240 int bytes;
248 byte[] bufferOut = new byte[1024]; 241 byte[] bufferOut = new byte[1024];
@@ -255,20 +248,18 @@ public class HttpClientUtil { @@ -255,20 +248,18 @@ public class HttpClientUtil {
255 out.write(foot); 248 out.write(foot);
256 out.flush(); 249 out.flush();
257 out.close(); 250 out.close();
258 - StringBuffer buffer = new StringBuffer(); 251 + StringBuilder sbuilder = new StringBuilder();
259 BufferedReader reader = null; 252 BufferedReader reader = null;
260 try { 253 try {
261 // 定义BufferedReader输入流来读取URL的响应 254 // 定义BufferedReader输入流来读取URL的响应
262 reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 255 reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
263 - String line = null; 256 + String line;
264 while ((line = reader.readLine()) != null) { 257 while ((line = reader.readLine()) != null) {
265 - buffer.append(line);  
266 - }  
267 - if (result == null) {  
268 - result = buffer.toString(); 258 + sbuilder.append(line);
269 } 259 }
  260 + result = sbuilder.toString();
270 } catch (IOException e) { 261 } catch (IOException e) {
271 - System.out.println("发送POST请求出现异常!" + e); 262 + System.out.println("httpPost error is " + e);
272 e.printStackTrace(); 263 e.printStackTrace();
273 } finally { 264 } finally {
274 if (reader != null) { 265 if (reader != null) {