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图片相关推荐

  1. vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式

    PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template><div><!--开启摄像头 ...

  2. react签名+上传base64图片接口入参处理

    好久没更博啦,最近写项目需要实现将签名生成的url(base64图片格式)上传到upload接口.在这简要的记录下-- 背景:1.实现签名功能: 2.上传base64图片(格式自定义) 实现:1.签名 ...

  3. vue移动端页面调用手机拍照_vue实现PC端调用摄像头拍照、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式...

    export default { () { return {} }, methods: { # // 压缩图片 and 旋转角度纠正 下方代码 # 需要自行去掉 个人只作为着色效果加上 compres ...

  4. 百度AI身份证识别接口,iOS上传base64图片报错216201问题解决办法总结

    1.首先我们查看百度文档,此错误码的介绍: 216201 image format error 上传的图片格式错误,现阶段我们支持的图片格式为:PNG.JPG.JPEG.BMP,请进行转码或更换图片 ...

  5. java读取服务器图片大小,SpringMVC中MultipartFile上传获取图片的宽度和高度详解

    SpringMVC一般使用MultipartFile来做文件的上传,通过MultipartFile的getContentType()方法判定文件的类型(MIME) ".doc":& ...

  6. App图片上传Base64图片上传

    /*** @Method base64图片上传* @author Angus* return array* date 2018-05-04*/ function saveBase64Image($ba ...

  7. 微信公众号开发《四》调用微信JS-SDK实现上传手机图片到服务器

    在这粘贴上三篇博文链接,方便大家查阅互相学习: 微信公众号开发<一>OAuth2.0网页授权认证获取用户的详细信息,实现自动登陆 微信公众号开发<二>发送模板消息实现消息业务实 ...

  8. js 头像上传(图片截取) 插件 全屏高清版 源码

    先上图片 index.html <!DOCTYPE html><html> <head><meta name="viewport" con ...

  9. java 模板接口开发_微信公众平台 发送模板消息(Java接口开发)

    前言:最近一直再弄微信扫码推送图文消息和模板消息发送,感觉学习到了不少东西.今天先总结一下微信公众平台模板消息的发送.因为这个自己弄了很久,开始很多地方不明白,所以今天好好总结一下. 微信公众平台技术 ...

最新文章

  1. 图像处理 区域删除_FotoWorks XL( 图像处理软件 )中文版分享
  2. 群晖备份linux分区,数据丢失的后悔药,群晖NAS备份方案详解
  3. 这个AI狠!能互译C++、Java和Python,以后只学一种编程就行吗
  4. ThinkPad R400 安装win2003网卡驱动
  5. 创新创业技术路线怎么写_怎么创业起步 如何写创业计划?
  6. Unity中使用gRPC
  7. 速度更新!GoCD又曝仨洞,极易遭利用且结合利用可成供应链攻击的新跳板
  8. html5游戏开发--动静结合(二)-用地图块拼成大地图 初探lufylegend
  9. THREEJS - 利用UV偏移模拟传送带运动
  10. python数据类型-列表练习
  11. DEDE织梦标签名称:{/dede:arclist} 详解
  12. 麻瓜编程python爬虫微专业_微专业:Python Web开发工程师(零基础课程),麻瓜编程侯爵主讲 价值2400元...
  13. 余世伟视频笔记----如何塑造管理者的性格魅力领袖根性之细心和胆识
  14. 明日之后怎么在电脑上玩 明日之后电脑版图文攻略
  15. vivo怎么切换为Android,vivox60pro如何切换系统 一键切换手机不通系统方法
  16. c语言des算法实验报告,c语言实现des算法des加密算法实验报告
  17. Pyhton可视化(1): 历年中国大学学术排行榜
  18. 微博舆情 之 特定话题情感分析
  19. 教育CRM究竟是什么?有何用处?
  20. ubuntu下彻底卸载 jupyter notebook

热门文章

  1. nodejs使用supervisor插件调试效率
  2. c语言实验数据类型体会,实验1-C语言开发环境使用和数据类型、运算符、表达式-实验总结与体会...
  3. linux基础命令游戏,LINUX-基础命令(三)
  4. Windows系统下使用Jenkins自动化发布.NET core程序到Linux平台下利用Docker快速启动
  5. Mac 配置PHP运行环境
  6. POJ 3461 Oulipo
  7. 状态空间搜索好题UVA10603
  8. 关键路径 - 数据结构和算法67
  9. DBGridEh使用技巧
  10. 图解Linux字符设备驱动