这是httpRequest方法[代码]/**[代码][代码] [代码][代码]* @param requestUrl 微信上传临时素材的接口url[代码][代码] [代码][代码]* @param file 要上传的文件[代码][代码] [代码][代码]* @return String 上传成功后,微信服务器返回的消息[代码][代码] [代码][代码]* @desc :微信上传素材的请求方法[代码][代码] [代码][代码]*/[代码][代码]public[代码] [代码]static[代码] [代码]String httpRequest(String requestUrl, File file) {[代码][代码] [代码][代码]StringBuffer buffer = [代码][代码]new[代码] [代码]StringBuffer();[代码] [代码] [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]//1.建立连接[代码][代码] [代码][代码]URL url = [代码][代码]new[代码] [代码]URL(requestUrl);[代码][代码] [代码][代码]HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); [代码][代码]//打开链接[代码] [代码] [代码][代码]//1.1输入输出设置[代码][代码] [代码][代码]httpUrlConn.setDoInput([代码][代码]true[代码][代码]);[代码][代码] [代码][代码]httpUrlConn.setDoOutput([代码][代码]true[代码][代码]);[代码][代码] [代码][代码]httpUrlConn.setUseCaches([代码][代码]false[代码][代码]); [代码][代码]// post方式不能使用缓存[代码][代码] [代码][代码]//1.2设置请求头信息[代码][代码] [代码][代码]httpUrlConn.setRequestProperty([代码][代码]"Connection"[代码][代码], [代码][代码]"Keep-Alive"[代码][代码]);[代码][代码] [代码][代码]httpUrlConn.setRequestProperty([代码][代码]"Charset"[代码][代码], [代码][代码]"UTF-8"[代码][代码]);[代码][代码] [代码][代码]//1.3设置边界[代码][代码] [代码][代码]String BOUNDARY = [代码][代码]"----------"[代码] [代码]+ System.currentTimeMillis();[代码][代码] [代码][代码]httpUrlConn.setRequestProperty([代码][代码]"Content-Type"[代码][代码], [代码][代码]"multipart/form-data; boundary="[代码] [代码]+ BOUNDARY);[代码] [代码] [代码][代码]// 请求正文信息[代码][代码] [代码][代码]// 第一部分:[代码][代码] [代码][代码]//2.将文件头输出到微信服务器[代码][代码] [代码][代码]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"[代码][代码]);[代码][代码] [代码][代码]// 获得输出流[代码][代码] [代码][代码]OutputStream outputStream = [代码][代码]new[代码] [代码]DataOutputStream(httpUrlConn.getOutputStream());[代码][代码] [代码][代码]// 将表头写入输出流中:输出表头[代码][代码] [代码][代码]outputStream.write(head);[代码] [代码] [代码][代码]//3.将文件正文部分输出到微信服务器[代码][代码] [代码][代码]// 把文件以流文件的方式 写入到微信服务器中[代码][代码] [代码][代码]DataInputStream in = [代码][代码]new[代码] [代码]DataInputStream([代码][代码]new[代码] [代码]FileInputStream(file));[代码][代码] [代码][代码]int[代码] [代码]bytes = [代码][代码]0[代码][代码];[代码][代码] [代码][代码]byte[代码][代码][] bufferOut = [代码][代码]new[代码] [代码]byte[代码][代码][[代码][代码]1024[代码][代码]];[代码][代码] [代码][代码]while[代码] [代码]((bytes = in.read(bufferOut)) != -[代码][代码]1[代码][代码]) {[代码][代码] [代码][代码]outputStream.write(bufferOut, [代码][代码]0[代码][代码], bytes);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]in.close();[代码][代码] [代码][代码]//4.将结尾部分输出到微信服务器[代码][代码] [代码][代码]byte[代码][代码][] foot = ([代码][代码]"\r\n--"[代码] [代码]+ BOUNDARY + [代码][代码]"--\r\n"[代码][代码]).getBytes([代码][代码]"utf-8"[代码][代码]);[代码][代码]// 定义最后数据分隔线[代码][代码] [代码][代码]outputStream.write(foot);[代码][代码] [代码][代码]outputStream.flush();[代码][代码] [代码][代码]outputStream.close();[代码] [代码] [代码][代码]//5.将微信服务器返回的输入流转换成字符串[代码][代码] [代码][代码]InputStream inputStream = httpUrlConn.getInputStream();[代码][代码] [代码][代码]InputStreamReader inputStreamReader = [代码][代码]new[代码] [代码]InputStreamReader(inputStream, [代码][代码]"utf-8"[代码][代码]);[代码][代码] [代码][代码]BufferedReader bufferedReader = [代码][代码]new[代码] [代码]BufferedReader(inputStreamReader);[代码] [代码] [代码][代码]String str = [代码][代码]null[代码][代码];[代码][代码] [代码][代码]while[代码] [代码]((str = bufferedReader.readLine()) != [代码][代码]null[代码][代码]) {[代码][代码] [代码][代码]buffer.append(str);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]bufferedReader.close();[代码][代码] [代码][代码]inputStreamReader.close();[代码][代码] [代码][代码]// 释放资源[代码][代码] [代码][代码]inputStream.close();[代码][代码] [代码][代码]inputStream = [代码][代码]null[代码][代码];[代码][代码] [代码][代码]httpUrlConn.disconnect();[代码] [代码] [代码][代码]} [代码][代码]catch[代码] [代码](IOException e) {[代码][代码] [代码][代码]System.out.println([代码][代码]"发送POST请求出现异常!"[代码] [代码]+ e);[代码][代码] [代码][代码]e.printStackTrace();[代码][代码] [代码][代码]}[代码][代码] [代码][代码]//4.json字符串转对象:解析返回值,json反序列化[代码][代码] [代码][代码]String result = buffer.toString().replaceAll([代码][代码]"[\\\\]"[代码][代码], [代码][代码]""[代码][代码]);[代码][代码] [代码][代码]System.out.println([代码][代码]"result:"[代码] [代码]+ result);[代码][代码] [代码][代码]JSONObject resultJSON = JSONObject.parseObject(result);[代码][代码] [代码][代码]return[代码] [代码]resultJSON.getString([代码][代码]"media_id"[代码][代码]);[代码][代码]}[代码]
微信客服自动回复图片?在调试微信客服自动回复图片时,所有都是按照文档要求开发,但是无法自动回复 首先:自动回复配置如下: [图片] 第二:验证加密(通过): [代码]boolean[代码] [代码]isGet = request.getMethod().toLowerCase().equals([代码][代码]"get"[代码][代码]);[代码][代码] [代码][代码]if[代码] [代码](isGet) {[代码][代码] [代码][代码]// 微信加密签名[代码][代码] [代码][代码]String signature = request.getParameter([代码][代码]"signature"[代码][代码]);[代码][代码] [代码][代码]// 时间戳[代码][代码] [代码][代码]String timestamp = request.getParameter([代码][代码]"timestamp"[代码][代码]);[代码][代码] [代码][代码]// 随机数[代码][代码] [代码][代码]String nonce = request.getParameter([代码][代码]"nonce"[代码][代码]);[代码][代码] [代码][代码]// 随机字符串[代码][代码] [代码][代码]String echostr = request.getParameter([代码][代码]"echostr"[代码][代码]);[代码] [代码] [代码][代码]// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败[代码][代码] [代码][代码]if[代码] [代码](signature != [代码][代码]null[代码] [代码]&& WeChatUtil.checkSignature(signature, timestamp, nonce)) {[代码][代码] [代码][代码]logger.debug([代码][代码]"signature == "[代码] [代码]+ signature);[代码][代码] [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]PrintWriter print = response.getWriter();[代码][代码] [代码][代码]print.write(echostr);[代码][代码] [代码][代码]print.flush();[代码][代码] [代码][代码]} [代码][代码]catch[代码] [代码](IOException e) {[代码][代码] [代码][代码]logger.error([代码][代码]"微信消息接收和token验证异常"[代码][代码], e);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码]第三:自动回复 自动回复先上传临时素材到回信服务,并获取mediaId [代码]//先上传临时素材到微信服务[代码] [代码] [代码][代码]String mediaId = uploadFileToWeChat(WeChatUtil.REQ_MESSAGE_TYPE_IMAGE);[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 上传图片到微信服务[代码][代码] [代码][代码]*[代码][代码] [代码][代码]* @param[代码][代码] [代码][代码]* @return[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]@Override[代码][代码] [代码][代码]public[代码] [代码]String uploadFileToWeChat(String type) {[代码][代码] [代码][代码]String accessToken = weiXinToken.getAccessToken();[代码][代码] [代码][代码]System.out.println([代码][代码]"token == "[代码] [代码]+ accessToken);[代码][代码] [代码][代码]RestTemplate restTemplate = [代码][代码]new[代码] [代码]RestTemplate();[代码][代码] [代码][代码]HttpHeaders headers = [代码][代码]new[代码] [代码]HttpHeaders();[代码][代码] [代码][代码]headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);[代码][代码] [代码][代码]File file = [代码][代码]new[代码] [代码]File(IMAGE_PATH);[代码][代码] [代码][代码]if[代码] [代码](!file.exists() || !file.isFile()) {[代码][代码] [代码][代码]return[代码] [代码]null[代码][代码];[代码][代码] [代码][代码]}[代码][代码] [代码][代码]StringBuilder sb = [代码][代码]new[代码] [代码]StringBuilder();[代码][代码] [代码][代码]sb.append(weixinImageUpload)[代码][代码] [代码][代码].append([代码][代码]"access_token="[代码][代码])[代码][代码] [代码][代码].append(accessToken)[代码][代码] [代码][代码].append([代码][代码]"&type="[代码][代码])[代码][代码] [代码][代码].append(type);[代码] [代码] [代码][代码]String mediaId = WeChatUtil.httpRequest(sb.toString(), file);[代码] [代码] [代码][代码]return[代码] [代码]mediaId;[代码][代码] [代码][代码]}[代码] 获取到mediaId后,然后自动回复开发 [代码]/**[代码][代码] [代码][代码]* 此处自动回复文本[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]TextMessageResponseVo textMessageResponseVo = [代码][代码]new[代码] [代码]TextMessageResponseVo();[代码][代码] [代码][代码]textMessageResponseVo.setFromUserName(requestMessageMap.get([代码][代码]"ToUserName"[代码][代码]));[代码][代码] [代码][代码]textMessageResponseVo.setToUserName(requestMessageMap.get([代码][代码]"FromUserName"[代码][代码]));[代码][代码] [代码][代码]textMessageResponseVo.setMsgType([代码][代码]"text"[代码][代码]);[代码][代码] [代码][代码]textMessageResponseVo.setCreateTime(System.currentTimeMillis() / [代码][代码]1000[代码][代码]);[代码][代码] [代码][代码]textMessageResponseVo.setContent([代码][代码]"你好"[代码][代码]);[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 此处自动回复图片[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]ImageMessageResponseVo imageMessageResponseVo = [代码][代码]new[代码] [代码]ImageMessageResponseVo();[代码][代码] [代码][代码]imageMessageResponseVo.setFromUserName(requestMessageMap.get([代码][代码]"FromUserName"[代码][代码]));[代码][代码] [代码][代码]imageMessageResponseVo.setToUserName(requestMessageMap.get([代码][代码]"ToUserName"[代码][代码]));[代码][代码] [代码][代码]imageMessageResponseVo.setMsgType([代码][代码]"image"[代码][代码]);[代码][代码] [代码][代码]imageMessageResponseVo.setCreateTime(System.currentTimeMillis() / [代码][代码]1000[代码][代码]);[代码][代码] [代码][代码]AutoAnswer autoAnswer = [代码][代码]new[代码] [代码]AutoAnswer();[代码][代码] [代码][代码]autoAnswer.setMediaId(mediaId);[代码][代码] [代码][代码]imageMessageResponseVo.setImage(autoAnswer);[代码][代码] [代码][代码]System.out.println([代码][代码]"textMessageResponseVo>>>>>>>>"[代码][代码]+imageMessageResponseVo.toString());[代码][代码] [代码][代码]String result = WeChatUtil.messageToXml(textMessageResponseVo);[代码][代码] [代码][代码]System.out.println([代码][代码]"result XML CDATA +++++++++++++++++++++++++"[代码] [代码]+ result);[代码][代码] [代码][代码]return[代码] [代码]result;[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 文本消息转换成xml[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]public[代码] [代码]static[代码] [代码]String messageToXml(TextMessageResponseVo textMessageResponseVo) {[代码][代码] [代码][代码]xstream.alias([代码][代码]"xml"[代码][代码], textMessageResponseVo.getClass());[代码][代码] [代码][代码]return[代码] [代码]xstream.toXML(textMessageResponseVo);[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 图片[代码][代码] [代码][代码]* @param imageMessageResponseVo[代码][代码] [代码][代码]* @return[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]public[代码] [代码]static[代码] [代码]String messageToXml(ImageMessageResponseVo imageMessageResponseVo) {[代码][代码] [代码][代码]xstream.alias([代码][代码]"xml"[代码][代码], imageMessageResponseVo.getClass());[代码][代码] [代码][代码]return[代码] [代码]xstream.toXML(imageMessageResponseVo);[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]/**[代码][代码] [代码][代码]* 扩展xstream使其支持CDATA[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]private[代码] [代码]static[代码] [代码]XStream xstream = [代码][代码]new[代码] [代码]XStream([代码][代码]new[代码] [代码]XppDriver() {[代码][代码] [代码][代码]@Override[代码][代码] [代码][代码]public[代码] [代码]HierarchicalStreamWriter createWriter(Writer out) {[代码][代码] [代码][代码]return[代码] [代码]new[代码] [代码]PrettyPrintWriter(out) {[代码][代码] [代码][代码]// 对所有xml节点的转换都增加CDATA标记[代码][代码] [代码][代码]boolean[代码] [代码]cdata = [代码][代码]true[代码][代码];[代码] [代码] [代码][代码]@Override[代码][代码] [代码][代码]@SuppressWarnings[代码][代码]([代码][代码]"unchecked"[代码][代码])[代码][代码] [代码][代码]public[代码] [代码]void[代码] [代码]startNode(String name, Class clazz) {[代码][代码] [代码][代码]super[代码][代码].startNode(name, clazz);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]@Override[代码][代码] [代码][代码]protected[代码] [代码]void[代码] [代码]writeText(QuickWriter writer, String text) {[代码][代码] [代码][代码]if[代码] [代码](cdata) {[代码][代码] [代码][代码]writer.write([代码][代码]"<![CDATA["[代码][代码]);[代码][代码] [代码][代码]writer.write(text);[代码][代码] [代码][代码]writer.write([代码][代码]"]]>"[代码][代码]);[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]writer.write(text);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]};[代码][代码] [代码][代码]}[代码][代码] [代码][代码]});[代码] 最后,响应微信服务的是一串String的xml [代码]//文本响应微信的xml[代码][代码]<xml>[代码][代码] [代码][代码]<ToUserName><![CDATA[onv1V4-zyxgliQlXbaBujrNT-ycs]]></ToUserName>[代码][代码] [代码][代码]<FromUserName><![CDATA[gh_dc0815388d0f]]></FromUserName>[代码][代码] [代码][代码]<CreateTime><![CDATA[[代码][代码]1570614951[代码][代码]]]></CreateTime>[代码][代码] [代码][代码]<MsgType><![CDATA[text]]></MsgType>[代码][代码] [代码][代码]<Content><![CDATA[你好]]></Content>[代码][代码]</xml>[代码][代码] [代码] [代码] [代码] [代码]//图片响应微信的xml[代码][代码] [代码] [代码]<xml>[代码] [代码] [代码][代码]<ToUserName><![CDATA[onv1V4-zyxgliQlXbaBujrNT-ycs]]></ToUserName>[代码] [代码] [代码][代码]<FromUserName><![CDATA[gh_dc0815388d0f]]></FromUserName>[代码] [代码] [代码][代码]<CreateTime><![CDATA[[代码][代码]1570670451[代码][代码]]]></CreateTime>[代码] [代码] [代码][代码]<MsgType><![CDATA[image]]></MsgType>[代码] [代码] [代码][代码]<Image>[代码] [代码] [代码][代码]<MediaId><![CDATA[ukM01FczEHwYUthTQ_Sf_6XNgI1_2lScqCFuIWOs8zQ8CGp0kgH98SS8B4SxdznU]]></MediaId>[代码] [代码] [代码][代码]</Image>[代码] [代码]</xml>[代码] [代码][代码] 但是最后没有收到微信自动回复,请求各位大佬指教 ;
2019-10-10