java 微信开发图片发送,微信开发?Java上传Base64图片
class="java">import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* 文件处理工具类
*/
public class FileHelpers {
private static final Logger logger = LogManager.getLogger(FileHelpers.class);
/**
* 解析base64格式图片,并保存到临时目录
*
* @param base64Str base64格式图片
* @param file 临时文件对象
* @return File Object
* @throws Exception
*/
public static void decodeBase64ToFile(File file,String base64Str) throws Exception {
OutputStream out = null;
try {
int prefixIndex = base64Str.indexOf(",");
byte[] buffer = Base64.decodeBase64(base64Str.substring(prefixIndex + 1));
out = new FileOutputStream(file);
out.write(buffer);
out.flush();
} catch (Exception e) {
logger.debug("decodeBase64ToFile raise exception:" + e.getMessage());
throw new Exception(e.getMessage());
} finally {
if (out!=null) {
out.close();
}
}
}
}
/*********************************************/
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.util.Map;
public class WeixinMediaUtils {
private final Logger logger = LogManager.getLogger(ServiceWeixinMedias.class);
/**
* 卡券 上传图片接口
*
* @param accessToken 开放平台/或者公众平台的accessToken
* @param imgStream 图片base64字符串
* @return 上传成功后,返回的图片Url
*/
public static Map uploadimg(String accessToken, String imgStream) {
File tempFile = null;
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
String url = String.format("%s%s",
"https://api.weixin.qq.com/cgi-bin/media/uploadimg",
"ACCESS_TOKEN="+accessToken))
);
tempFile = File.createTempFile("weixin_media", ".jpg");
FileHelpers.decodeBase64ToFile(tempFile, imgStream);
HttpPost httpPost = new HttpPost(url);
FileBody fileBody = new FileBody(tempFile);
HttpEntity httpEntity = MultipartEntityBuilder.create().addPart("buffer", fileBody).build();
httpPost.setEntity(httpEntity);
httpClient = HttpClients.createDefault();
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
String responseEntityStr = EntityUtils.toString(response.getEntity());
System.out.println(responseEntityStr);
System.out.println("Response content length: " + responseEntity.getContentLength());
return JsonHelpers.deserializeToMap(responseEntityStr).orElseThrow(() -> new Exception("uploadimg failed,response realizeToMap raise Exception"));
} else {
throw new Exception(String.format("call weixin interface fail,file 【%s】 upload fail", tempFile.getAbsolutePath()));
}
} catch (Throwable t) {
t.printStackTrace();
throw new Exception(t.getMessage());
} finally {
if (tempFile!=null) {
tempFile.delete();
}
if (httpClient!=null) {
HttpClientUtils.closeQuietly(httpClient);
}
if (response!=null) {
HttpClientUtils.closeQuietly(response);
}
}
}
? ? Java使用HttpClient,将Base64图片上传到微信!!
? ? 诸多不完善之处,敬请指正!
java 微信开发图片发送,微信开发?Java上传Base64图片相关推荐
- vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式
PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template><div><!--开启摄像头 ...
- react签名+上传base64图片接口入参处理
好久没更博啦,最近写项目需要实现将签名生成的url(base64图片格式)上传到upload接口.在这简要的记录下-- 背景:1.实现签名功能: 2.上传base64图片(格式自定义) 实现:1.签名 ...
- vue移动端页面调用手机拍照_vue实现PC端调用摄像头拍照、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式...
export default { () { return {} }, methods: { # // 压缩图片 and 旋转角度纠正 下方代码 # 需要自行去掉 个人只作为着色效果加上 compres ...
- 百度AI身份证识别接口,iOS上传base64图片报错216201问题解决办法总结
1.首先我们查看百度文档,此错误码的介绍: 216201 image format error 上传的图片格式错误,现阶段我们支持的图片格式为:PNG.JPG.JPEG.BMP,请进行转码或更换图片 ...
- java读取服务器图片大小,SpringMVC中MultipartFile上传获取图片的宽度和高度详解
SpringMVC一般使用MultipartFile来做文件的上传,通过MultipartFile的getContentType()方法判定文件的类型(MIME) ".doc":& ...
- App图片上传Base64图片上传
/*** @Method base64图片上传* @author Angus* return array* date 2018-05-04*/ function saveBase64Image($ba ...
- 微信公众号开发《四》调用微信JS-SDK实现上传手机图片到服务器
在这粘贴上三篇博文链接,方便大家查阅互相学习: 微信公众号开发<一>OAuth2.0网页授权认证获取用户的详细信息,实现自动登陆 微信公众号开发<二>发送模板消息实现消息业务实 ...
- js 头像上传(图片截取) 插件 全屏高清版 源码
先上图片 index.html <!DOCTYPE html><html> <head><meta name="viewport" con ...
- java 模板接口开发_微信公众平台 发送模板消息(Java接口开发)
前言:最近一直再弄微信扫码推送图文消息和模板消息发送,感觉学习到了不少东西.今天先总结一下微信公众平台模板消息的发送.因为这个自己弄了很久,开始很多地方不明白,所以今天好好总结一下. 微信公众平台技术 ...
最新文章
- 图像处理 区域删除_FotoWorks XL( 图像处理软件 )中文版分享
- 群晖备份linux分区,数据丢失的后悔药,群晖NAS备份方案详解
- 这个AI狠!能互译C++、Java和Python,以后只学一种编程就行吗
- ThinkPad R400 安装win2003网卡驱动
- 创新创业技术路线怎么写_怎么创业起步 如何写创业计划?
- Unity中使用gRPC
- 速度更新!GoCD又曝仨洞,极易遭利用且结合利用可成供应链攻击的新跳板
- html5游戏开发--动静结合(二)-用地图块拼成大地图 初探lufylegend
- THREEJS - 利用UV偏移模拟传送带运动
- python数据类型-列表练习
- DEDE织梦标签名称:{/dede:arclist} 详解
- 麻瓜编程python爬虫微专业_微专业:Python Web开发工程师(零基础课程),麻瓜编程侯爵主讲 价值2400元...
- 余世伟视频笔记----如何塑造管理者的性格魅力领袖根性之细心和胆识
- 明日之后怎么在电脑上玩 明日之后电脑版图文攻略
- vivo怎么切换为Android,vivox60pro如何切换系统 一键切换手机不通系统方法
- c语言des算法实验报告,c语言实现des算法des加密算法实验报告
- Pyhton可视化(1): 历年中国大学学术排行榜
- 微博舆情 之 特定话题情感分析
- 教育CRM究竟是什么?有何用处?
- ubuntu下彻底卸载 jupyter notebook