日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

微信素材獲取(JAVA)

 印度阿三17 2019-08-30

public void getMaterial(HttpServletRequest request, HttpServletResponse response) throws Exception {

String csid = request.getParameter("csid");
String croptype = request.getParameter("croptype");
String mediaId = request.getParameter("mediaid");
String appid = sysWxBindCorpService.getMpAppIdByCsid(csid, croptype);

InputStream inputStream = weChatMaterialUpload.getMaterialByMediaId(appid, mediaId);

//輸出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 1024;
byte tmp[] = new byte[len];
int i;
while ((i = inputStream.read(tmp, 0, len)) > 0) {
baos.write(tmp, 0, i);
}

byte imgs[] = baos.toByteArray();
OutputStream stream = response.getOutputStream();
stream.write(imgs);
stream.flush();
stream.close();

// 循環(huán)取出流中的數(shù)據(jù),保存
/*
* byte[] data = new byte[1024]; int len = 0; FileOutputStream fileOutputStream
* = null;
*
* String filePath = "D:\\ztech-server\\upload\\A.PNG";
*
* try { fileOutputStream = new FileOutputStream(filePath); while ((len =
* inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, len); } }
* catch (IOException e) { } finally { if (inputStream != null) { try {
* inputStream.close(); } catch (IOException e) { } } if (fileOutputStream !=
* null) { try { fileOutputStream.close(); } catch (IOException e) { } } }
*/

}

public InputStream getMaterialByMediaId(String appid, String mediaId) throws Exception {

try {
String charset = "UTF-8";
String url = MessageFormat.format(GET_MATERIAL_URL, accessToken.getAccessToken(appid));
Map<String, String> baseParam = new HashMap<>();
baseParam.put("media_id", mediaId);
byte[] bytes = JSONUtils.toJSONString(baseParam).getBytes(charset);
InputStream inputResult = WeChatHttpUtil.Instance().doPostIn(url, bytes, charset, "");

/*InputStream轉(zhuǎn)換成ByteArrayOutputStream 解決InputStream只能讀取一次問題(ByteArrayOutputStream轉(zhuǎn)成多個(gè)InputStream分別讀取)*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputResult.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
/*InputStream轉(zhuǎn)換成ByteArrayOutputStream 解決InputStream只能讀取一次問題(ByteArrayOutputStream轉(zhuǎn)成多個(gè)InputStream分別讀?。?/

inputResult = new ByteArrayInputStream(baos.toByteArray());
InputStream backResult = new ByteArrayInputStream(baos.toByteArray());

String result = IOUtil.toString(backResult, charset);
try {
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject != null && jsonObject.containsKey("errcode") && (jsonObject.get("errcode").equals(40001) || jsonObject.get("errcode").equals(42001)
|| jsonObject.get("errcode").equals(41001) || jsonObject.get("errcode").equals(40014))) {

// 清除微信token緩存并重試請(qǐng)求
WxTokenCache.setAccessToken(appid, "");
inputResult = WeChatHttpUtil.Instance().doPostIn(url, bytes, charset, "");

}
} catch (Exception e) {
// TODO: handle exception
}

logger.info("getMaterialByMediaId獲取素材詳情:" result);
return inputResult;
} catch (Exception e) {
logger.error("getMaterialByMediaId:" e.getMessage());
e.printStackTrace();
return null;
}

}

?

public InputStream doPostIn(String url, byte[] params, String charset, String cookie) throws IOException {
URL u = new URL(url);
HttpURLConnection conn = null;
InputStream in = null;
try {
conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("POST");
if (cookie != null) {
conn.setRequestProperty("Cookie", cookie);
}
conn.setConnectTimeout(TIMEOUT_MILLIS);
conn.setReadTimeout(TIMEOUT_MILLIS);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();

if (params != null) {
OutputStream out = conn.getOutputStream();
out.write(params);
out.flush();
out.close();
}

int status = conn.getResponseCode();
if (status >= HttpURLConnection.HTTP_BAD_REQUEST) {
in = conn.getErrorStream();
} else {
in = conn.getInputStream();
}

} catch (IOException e) {
throw e;
} finally {

}
return in;
}

來源:https://www./content-1-424951.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多