首页 / JAVA / 微信素材获取(JAVA)
微信素材获取(JAVA)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了微信素材获取(JAVA),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4035字,纯文字阅读大概需要6分钟。
内容图文
![微信素材获取(JAVA)](/upload/InfoBanner/zyjiaocheng/725/bb0e849aab86403f86ad2c449619640b.jpg)
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();
// 循环取出流中的数据,保存
/*
* 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转换成ByteArrayOutputStream 解决InputStream只能读取一次问题(ByteArrayOutputStream转成多个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转换成ByteArrayOutputStream 解决InputStream只能读取一次问题(ByteArrayOutputStream转成多个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缓存并重试请求
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;
}
内容总结
以上是互联网集市为您收集整理的微信素材获取(JAVA)全部内容,希望文章能够帮你解决微信素材获取(JAVA)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。