Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cn.com.sunjiesh.wechat.handler; import cn.com.sunjiesh.utils.thirdparty.base.HttpService; import cn.com.sunjiesh.xcutils.common.base.ServiceException; import com.alibaba.fastjson.JSONObject; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ?Handler * * @author tom */ public class WechatBaseHandler { private static final Logger LOGGER = LoggerFactory.getLogger(WechatBaseHandler.class); private static final String DOWNLOAD_FILE_ERROR = "?"; /** * ?? * * @param url ? * @param accessToken accessToken * @return ??URL? */ public static String getRequestUrl(String url, String accessToken) { String requestUrl = url; requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken); return requestUrl; } /** * ? * * @param httpResponse httpResponse * @param fileTpye fileTpye * @return File * @throws ServiceException ServiceException */ public static File getFileResponseFromHttpResponse(HttpResponse httpResponse, String fileTpye) throws ServiceException { File file = null; try { StatusLine httpStatusLine = httpResponse.getStatusLine(); HttpEntity httpEntity = httpResponse.getEntity(); int statusCode = httpStatusLine.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { LOGGER.error(DOWNLOAD_FILE_ERROR); throw new ServiceException(DOWNLOAD_FILE_ERROR); } String fileName = UUID.randomUUID().toString(); fileName = fileName + "." + fileTpye; InputStream is = httpEntity.getContent(); if (!StringUtils.isEmpty(fileName)) { //?? file = new File("/var/tmp", fileName); if (file.exists()) { file.delete(); } FileOutputStream fos = new FileOutputStream(file, false); FileChannel fileChannel = fos.getChannel(); ByteBuffer byteBuffer = null; byte[] tempBytes = new byte[4096]; while (is.read(tempBytes) > 0) { byteBuffer = ByteBuffer.wrap(tempBytes); fileChannel.write(byteBuffer); } if (fos != null) { fos.close(); } fileChannel.close(); } } catch (IllegalStateException e) { LOGGER.error(DOWNLOAD_FILE_ERROR, e); throw new ServiceException(DOWNLOAD_FILE_ERROR, e); } catch (FileNotFoundException e) { LOGGER.error(DOWNLOAD_FILE_ERROR, e); throw new ServiceException(DOWNLOAD_FILE_ERROR, e); } catch (IOException e) { LOGGER.error(DOWNLOAD_FILE_ERROR, e); throw new ServiceException(DOWNLOAD_FILE_ERROR, e); } return file; } /** * Http Post??JSON * * @param url url? * @param jsonObject jsonObject * @return JSONObject * @throws ServiceException ServiceException */ public static JSONObject getWxJSONObjectResponseFromHttpPostMethod(String url, JSONObject jsonObject) throws ServiceException { JSONObject responseJson = new HttpService().getJSONObjectResponseFromHttpPostMethod(url, jsonObject); validWxJsonObject(responseJson); return responseJson; } /** * Http Get ??JSON * * @param url url? * @return JSONObject * @throws ServiceException ServiceException */ public static JSONObject getWxJSONObjectResponseFromHttpGetMethod(String url) throws ServiceException { JSONObject responseJson = new HttpService().getJSONObjectResponseFromHttpGetMethod(url); validWxJsonObject(responseJson); return responseJson; } /** * Http Post??JSON * * @param url url? * @param file * @return JSONObject * @throws ServiceException ServiceException */ public static JSONObject getWxJSONObjectResponseFromHttpPostMethod(String url, File file) throws ServiceException { JSONObject responseJson = new HttpService().getJSONObjectResponseFromHttpPostMethod(url, file); validWxJsonObject(responseJson); return responseJson; } /** * ?JSON * * @param responseJson json * @throws ServiceException ServiceException */ private static void validWxJsonObject(JSONObject responseJson) throws ServiceException { if (null == responseJson) { throw new ServiceException("JSONObject"); } // ? if (responseJson.containsKey("errcode")) { String errmsg = responseJson.getString("errmsg"); int errcode = responseJson.getIntValue("errcode"); if (errcode != 0) { //? Map<String, String> errorMsgMap = initErrorMsgMap(); String wechatErrorMsg = errorMsgMap.get(String.valueOf(errcode)); if (StringUtils.isEmpty(wechatErrorMsg)) { wechatErrorMsg = errmsg; } LOGGER.error("?" + wechatErrorMsg); throw new ServiceException(errmsg); } } } /** * Http Post??JSON * * @param url ? * @param fileParamKeys ?Key * @param files fileParamKeys * @return JSONObject * @throws ServiceException ServiceException */ public static JSONObject getWxJSONObjectResponseFromHttpPostMethod(String url, String[] fileParamKeys, File[] files) throws ServiceException { JSONObject responseJson = new HttpService().getJSONObjectResponseFromHttpPostMethod(url, fileParamKeys, files); validWxJsonObject(responseJson); return responseJson; } public static JSONObject getWxJSONObjectResponseFromHttpPostMethod(String url, String[] fileParamKeys, File[] files, String[] fieldParamKeys, String[] fieldParamValues) throws ServiceException { JSONObject responseJson = new HttpService().getJSONObjectResponseFromHttpPostMethod(url, fileParamKeys, files, fieldParamKeys, fieldParamValues); validWxJsonObject(responseJson); return responseJson; } /** * ??Map * * @return ?Map */ private static Map<String, String> initErrorMsgMap() { Map<String, String> errorMsgMap = new HashMap<String, String>(); errorMsgMap.put("-1", "????"); errorMsgMap.put("0", "?"); errorMsgMap.put("40001", "?access_tokenAppSecretaccess_token?AppSecret????"); errorMsgMap.put("40002", "???"); errorMsgMap.put("40003", "??OpenID?OpenID????OpenID"); errorMsgMap.put("40004", "??"); errorMsgMap.put("40005", "??"); errorMsgMap.put("40006", "???"); errorMsgMap.put("40007", "??id"); errorMsgMap.put("40008", "???"); errorMsgMap.put("40009", "???"); errorMsgMap.put("40010", "???"); errorMsgMap.put("40011", "???"); errorMsgMap.put("40012", "???"); errorMsgMap.put("40013", "??AppID?AppID????"); errorMsgMap.put("40014", "??access_token?access_token?????"); errorMsgMap.put("40015", "????"); errorMsgMap.put("40016", "??"); errorMsgMap.put("40017", "??"); errorMsgMap.put("40018", "????"); errorMsgMap.put("40019", "??KEY"); errorMsgMap.put("40020", "??URL"); errorMsgMap.put("40021", "?????"); errorMsgMap.put("40022", "?????"); errorMsgMap.put("40023", "?????"); errorMsgMap.put("40024", "?????"); errorMsgMap.put("40025", "???????"); errorMsgMap.put("40026", "?????KEY"); errorMsgMap.put("40027", "?????URL"); errorMsgMap.put("40028", "????"); errorMsgMap.put("40029", "??oauth_code"); errorMsgMap.put("40030", "??refresh_token"); errorMsgMap.put("40031", "??openid"); errorMsgMap.put("40032", "??openid"); errorMsgMap.put("40033", "????\\uxxxx?"); errorMsgMap.put("40035", "???"); errorMsgMap.put("40038", "???"); errorMsgMap.put("40039", "??URL"); errorMsgMap.put("40050", "??id"); errorMsgMap.put("40051", "????"); errorMsgMap.put("40117", "????"); errorMsgMap.put("40118", "media_id???"); errorMsgMap.put("40119", "button"); errorMsgMap.put("40120", "button"); errorMsgMap.put("40121", "??media_id"); errorMsgMap.put("40132", "???"); errorMsgMap.put("40137", "???"); errorMsgMap.put("41001", "access_token?"); errorMsgMap.put("41002", "appid?"); errorMsgMap.put("41003", "refresh_token?"); errorMsgMap.put("41004", "secret?"); errorMsgMap.put("41005", "?"); errorMsgMap.put("41006", "media_id?"); errorMsgMap.put("41007", "????"); errorMsgMap.put("41008", "oauth code"); errorMsgMap.put("41009", "openid"); errorMsgMap.put("42001", "access_tokenaccess_token??-?access_tokenaccess_token"); errorMsgMap.put("42002", "refresh_token"); errorMsgMap.put("42003", "oauth_code"); errorMsgMap.put("43001", "?GET"); errorMsgMap.put("43002", "?POST"); errorMsgMap.put("43003", "?HTTPS"); errorMsgMap.put("43004", "?"); errorMsgMap.put("43005", "??"); errorMsgMap.put("44001", ""); errorMsgMap.put("44002", "POST?"); errorMsgMap.put("44003", "?"); errorMsgMap.put("44004", "?"); errorMsgMap.put("45001", "??"); errorMsgMap.put("45002", "??"); errorMsgMap.put("45003", "?"); errorMsgMap.put("45004", "???"); errorMsgMap.put("45005", "?"); errorMsgMap.put("45006", "?"); errorMsgMap.put("45007", "?"); errorMsgMap.put("45008", "??"); errorMsgMap.put("45009", "??"); errorMsgMap.put("45010", "???"); errorMsgMap.put("45015", "??"); errorMsgMap.put("45016", "??"); errorMsgMap.put("45017", "??"); errorMsgMap.put("45018", "??"); errorMsgMap.put("46001", "??"); errorMsgMap.put("46002", "???"); errorMsgMap.put("46003", "????"); errorMsgMap.put("46004", "?"); errorMsgMap.put("47001", "?JSON/XML"); errorMsgMap.put("48001", "api?????-????"); errorMsgMap.put("50001", "?api"); errorMsgMap.put("50002", "????????"); errorMsgMap.put("61451", "?(invalid parameter);"); errorMsgMap.put("61452", "??(invalid kf_account);"); errorMsgMap.put("61453", "???(kf_account exsited);"); errorMsgMap.put("61454", "??????(?10?@?@???);(invalid kf_acount length);"); errorMsgMap.put("61455", "???????(?+);(illegal character in kf_account);"); errorMsgMap.put("61456", "????(10??);(kf_account count exceeded);"); errorMsgMap.put("61457", "?(invalid file type);"); errorMsgMap.put("61450", "(system error);"); errorMsgMap.put("61500", "?"); errorMsgMap.put("61501", ""); errorMsgMap.put("9001001", "POST????"); errorMsgMap.put("9001002", "???"); errorMsgMap.put("9001003", "Ticket??"); errorMsgMap.put("9001004", "??"); errorMsgMap.put("9001005", "??"); errorMsgMap.put("9001006", "?OpenID"); errorMsgMap.put("9001007", ""); errorMsgMap.put("9001008", "????"); errorMsgMap.put("9001009", "????"); errorMsgMap.put("9001010", ""); errorMsgMap.put("9001020", "????"); errorMsgMap.put("9001021", "50%?"); errorMsgMap.put("9001022", "??0"); errorMsgMap.put("9001023", "ID"); errorMsgMap.put("9001024", "ID??50"); errorMsgMap.put("9001025", "ID??"); errorMsgMap.put("9001026", "?ID??"); errorMsgMap.put("9001027", "????"); errorMsgMap.put("9001028", "?ID??10"); errorMsgMap.put("9001029", "??"); errorMsgMap.put("9001030", "?ID??50"); errorMsgMap.put("9001031", "??"); errorMsgMap.put("9001032", "???"); errorMsgMap.put("9001033", "ID??"); errorMsgMap.put("9001034", "?"); errorMsgMap.put("9001035", "???"); errorMsgMap.put("9001036", "begin??"); return errorMsgMap; } }