|
1 | 1 | package cn.zhouyafeng.itchat4j.api; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.text.SimpleDateFormat; |
3 | 5 | import java.util.Date; |
4 | 6 | import java.util.HashMap; |
5 | 7 | import java.util.Map; |
| 8 | +import java.util.Random; |
6 | 9 | import java.util.logging.Logger; |
7 | 10 | import java.util.regex.Matcher; |
8 | 11 |
|
| 12 | +import javax.activation.MimetypesFileTypeMap; |
| 13 | + |
9 | 14 | import org.apache.http.HttpEntity; |
| 15 | +import org.apache.http.entity.ContentType; |
| 16 | +import org.apache.http.entity.mime.HttpMultipartMode; |
| 17 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
10 | 18 | import org.apache.http.util.EntityUtils; |
11 | 19 |
|
12 | 20 | import com.alibaba.fastjson.JSON; |
@@ -166,7 +174,155 @@ public static void sendRawMsg(int msgType, String content, String toUserName) { |
166 | 174 | } catch (Exception e) { |
167 | 175 | logger.info(e.getMessage()); |
168 | 176 | } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * 上传多媒体文件到 微信服务器,目前应该支持3种类型: |
| 181 | + * <p> |
| 182 | + * 1. pic 直接显示,包含图片,表情 |
| 183 | + * </p> |
| 184 | + * <p> |
| 185 | + * 2.video |
| 186 | + * </p> |
| 187 | + * <p> |
| 188 | + * 3.doc 显示为文件,包含PDF等 |
| 189 | + * </p> |
| 190 | + * |
| 191 | + * @author https://github.com/yaphone |
| 192 | + * @date 2017年5月7日 上午12:41:13 |
| 193 | + * @param filePath |
| 194 | + * @return |
| 195 | + */ |
| 196 | + private static String uploadMediaToServer(String filePath) { |
| 197 | + File f = new File(filePath); |
| 198 | + if (!f.exists() && f.isFile()) { |
| 199 | + logger.info("file is not exist"); |
| 200 | + return null; |
| 201 | + } |
| 202 | + String url = (String) core.getLoginInfo().get("fileUrl") + "/webwxuploadmedia?f=json"; |
| 203 | + String mimeType = new MimetypesFileTypeMap().getContentType(f); |
| 204 | + String mediaType = ""; |
| 205 | + if (mimeType == null) { |
| 206 | + mimeType = "text/plain"; |
| 207 | + } else { |
| 208 | + mediaType = mimeType.split("/")[0].equals("image") ? "pic" : "doc"; |
| 209 | + } |
| 210 | + String lastModifieDate = new SimpleDateFormat("yyyy MM dd HH:mm:ss").format(new Date()); |
| 211 | + long fileSize = f.length(); |
| 212 | + String passTicket = (String) core.getLoginInfo().get("pass_ticket"); |
| 213 | + String clientMediaId = String.valueOf(new Date().getTime()) |
| 214 | + + String.valueOf(new Random().nextLong()).substring(0, 4); |
| 215 | + String webwxDataTicket = MyHttpClient.getCookie("webwx_data_ticket"); |
| 216 | + if (webwxDataTicket == null) { |
| 217 | + logger.info("get cookie webwx_data_ticket error"); |
| 218 | + return null; |
| 219 | + } |
| 220 | + |
| 221 | + Map<String, Object> paramMap = new HashMap<String, Object>(); |
| 222 | + @SuppressWarnings("unchecked") |
| 223 | + Map<String, Map<String, String>> baseRequestMap = (Map<String, Map<String, String>>) core.getLoginInfo() |
| 224 | + .get("baseRequest"); |
| 225 | + paramMap.put("BaseRequest", baseRequestMap.get("BaseRequest")); |
| 226 | + paramMap.put("ClientMediaId", clientMediaId); |
| 227 | + paramMap.put("TotalLen", fileSize); |
| 228 | + paramMap.put("StartPos", 0); |
| 229 | + paramMap.put("DataLen", fileSize); |
| 230 | + paramMap.put("MediaType", 4); |
| 231 | + |
| 232 | + MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
| 233 | + builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 234 | + |
| 235 | + builder.addTextBody("id", "WU_FILE_0", ContentType.TEXT_PLAIN); |
| 236 | + builder.addTextBody("name", filePath, ContentType.TEXT_PLAIN); |
| 237 | + builder.addTextBody("type", mimeType, ContentType.TEXT_PLAIN); |
| 238 | + builder.addTextBody("lastModifieDate", lastModifieDate, ContentType.TEXT_PLAIN); |
| 239 | + builder.addTextBody("size", String.valueOf(fileSize), ContentType.TEXT_PLAIN); |
| 240 | + builder.addTextBody("mediatype", mediaType, ContentType.TEXT_PLAIN); |
| 241 | + builder.addTextBody("uploadmediarequest", JSON.toJSONString(paramMap), ContentType.TEXT_PLAIN); |
| 242 | + builder.addTextBody("webwx_data_ticket", webwxDataTicket, ContentType.TEXT_PLAIN); |
| 243 | + builder.addTextBody("pass_ticket", passTicket, ContentType.TEXT_PLAIN); |
| 244 | + builder.addBinaryBody("filename", f, ContentType.create(mimeType), filePath); |
| 245 | + HttpEntity reqEntity = builder.build(); |
| 246 | + HttpEntity entity = myHttpClient.doPostFile(url, reqEntity); |
| 247 | + if (entity != null) { |
| 248 | + try { |
| 249 | + String result = EntityUtils.toString(entity, "UTF-8"); |
| 250 | + return JSON.parseObject(result).getString("MediaId"); |
| 251 | + } catch (Exception e) { |
| 252 | + logger.info(e.getMessage()); |
| 253 | + } |
| 254 | + |
| 255 | + } |
| 256 | + return null; |
| 257 | + } |
169 | 258 |
|
| 259 | + /** |
| 260 | + * 发送图片消息,内部调用 |
| 261 | + * |
| 262 | + * @author https://github.com/yaphone |
| 263 | + * @date 2017年5月7日 下午10:38:55 |
| 264 | + * @return |
| 265 | + */ |
| 266 | + private static boolean webWxSendMsgImg(String userId, String mediaId) { |
| 267 | + String url = String.format("%s/webwxstatusnotify?lang=zh_CN&pass_ticket=%s", core.getLoginInfo().get("url"), |
| 268 | + core.getLoginInfo().get("pass_ticket")); |
| 269 | + Map<String, Object> msgMap = new HashMap<String, Object>(); |
| 270 | + msgMap.put("Type", 3); |
| 271 | + msgMap.put("MediaId", mediaId); |
| 272 | + msgMap.put("FromUserName", core.getUserSelfList().get(0).getString("UserName")); |
| 273 | + msgMap.put("ToUserName", userId); |
| 274 | + String clientMsgId = String.valueOf(new Date().getTime()) |
| 275 | + + String.valueOf(new Random().nextLong()).substring(1, 5); |
| 276 | + msgMap.put("LocalID", clientMsgId); |
| 277 | + msgMap.put("ClientMsgId", clientMsgId); |
| 278 | + Map<String, Object> paramMap = new HashMap<String, Object>(); |
| 279 | + @SuppressWarnings("unchecked") |
| 280 | + Map<String, Map<String, String>> baseRequestMap = (Map<String, Map<String, String>>) core.getLoginInfo() |
| 281 | + .get("baseRequest"); |
| 282 | + paramMap.put("BaseRequest", baseRequestMap.get("BaseRequest")); |
| 283 | + paramMap.put("Msg", msgMap); |
| 284 | + String paramStr = JSON.toJSONString(paramMap); |
| 285 | + System.out.println(url); |
| 286 | + System.out.println(paramStr); |
| 287 | + HttpEntity entity = myHttpClient.doPost(url, paramStr); |
| 288 | + if (entity != null) { |
| 289 | + try { |
| 290 | + System.out.println(EntityUtils.toString(entity, "UTF-8")); |
| 291 | + } catch (Exception e) { |
| 292 | + logger.info(e.getMessage()); |
| 293 | + } |
| 294 | + } |
| 295 | + return true; |
| 296 | + |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * 根据昵称发送图片消息 |
| 301 | + * |
| 302 | + * @author https://github.com/yaphone |
| 303 | + * @date 2017年5月7日 下午10:32:45 |
| 304 | + * @param nackName |
| 305 | + * @return |
| 306 | + */ |
| 307 | + public static boolean sendPicMsgByNickName(String nickName, String filePath) { |
| 308 | + return false; |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * 根据用户id发送图片消息 |
| 313 | + * |
| 314 | + * @author https://github.com/yaphone |
| 315 | + * @date 2017年5月7日 下午10:34:24 |
| 316 | + * @param nickName |
| 317 | + * @param filePath |
| 318 | + * @return |
| 319 | + */ |
| 320 | + public static boolean sendPicMsgByUserId(String userId, String filePath) { |
| 321 | + String mediaId = uploadMediaToServer(filePath); |
| 322 | + if (mediaId != null) { |
| 323 | + return webWxSendMsgImg(userId, mediaId); |
| 324 | + } |
| 325 | + return false; |
170 | 326 | } |
171 | 327 |
|
172 | 328 | } |
0 commit comments