1.控制层

    /*** 上传临时素材*/@ResponseBody@PostMapping("/uploadResource")public BaseResponse uploadResource(MultipartHttpServletRequest param,@RequestParam("accessToken") String token,@RequestParam("type")String type) throws Exception{return BaseResponse.Builder.build(weixinService.uploadResource(param,token,type));}

2.Service层

    /*** 上传临时素材* @param param* @return*/Map<String, Object> uploadResource(MultipartHttpServletRequest param,String accessToken,String type)throws Exception ;

3.实现层

    @Overridepublic Map<String, Object> uploadResource(MultipartHttpServletRequest param,String accessToken,String type)throws Exception  {/*** 1.封装Multipart参数*/MultipartFile file = param.getFile("file");/*** 2.调用上传临时素材接口*/String result = uploadFile(M2F(file), type, accessToken);log.info("getMessageResult result=" + result);/*** 3.返回上传临时素材接口响应数据*/Map<String,Object> map=new HashMap<>();JSONObject resultObject = new JSONObject(result);map.put("errcode",resultObject.getInt("errcode"));map.put("errmsg",resultObject.get("errmsg"));map.put("media_id",resultObject.get("media_id"));return map;}/*** MultipartFile文件转File文件* @param file* @return* @throws Exception*/public File M2F(MultipartFile file) throws Exception {File f=File.createTempFile(UUID.randomUUID().toString(), "." + FilenameUtils.getExtension(file.getOriginalFilename()));file.transferTo(f);return f;}/*** 处理请求数据* @param file* @param fileType * @param accessToken* @return*/public String uploadFile(File file, String fileType,String accessToken){RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token="+accessToken+"&type="+fileType;HttpHeaders headers = new HttpHeaders();MediaType type = MediaType.parseMediaType("multipart/form-data");headers.setContentType(type);headers.setContentLength(file.length());headers.setContentDispositionFormData("media",file.getName());MultiValueMap<String,Object> param = new LinkedMultiValueMap<>();FileSystemResource resource = new FileSystemResource(file.getPath());param.add("file",resource);HttpEntity<MultiValueMap<String,Object>> formEntity = new HttpEntity<>(param, headers);ResponseEntity<String> data = restTemplate.postForEntity(url,formEntity,String.class);String body = data.getBody();return body;}

4.HttpClient类

import org.springframework.http.client.SimpleClientHttpRequestFactory;import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;/*** @author fcx* @create 2022-04-15 10:37**/
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {@Overrideprotected void prepareConnection(HttpURLConnection connection, String httpMethod) {try {if (!(connection instanceof HttpsURLConnection)) {throw new RuntimeException("An instance of HttpsURLConnection is expected");}HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] x509Certificates, String s) throwsCertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}}};SSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(null, trustAllCerts, new java.security.SecureRandom());httpsConnection.setSSLSocketFactory(new MyCustomSSLSocketFactory(sslContext.getSocketFactory()));httpsConnection.setHostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String s, SSLSession sslSession) {return true;}});super.prepareConnection(httpsConnection, httpMethod);} catch (Exception e) {e.printStackTrace();}}/*** We need to invoke sslSocket.setEnabledProtocols(new String[] {"SSLv3"});* see http://www.oracle.com/technetwork/java/javase/documentation/cve-2014-3566-2342133.html (Java 8 section)*/// SSLSocketFactory用于创建 SSLSocketsprivate static class MyCustomSSLSocketFactory extends SSLSocketFactory {private final SSLSocketFactory delegate;public MyCustomSSLSocketFactory(SSLSocketFactory delegate) {this.delegate = delegate;}// 返回默认启用的密码套件。除非一个列表启用,对SSL连接的握手会使用这些密码套件。// 这些默认的服务的最低质量要求保密保护和服务器身份验证@Overridepublic String[] getDefaultCipherSuites() {return delegate.getDefaultCipherSuites();}// 返回的密码套件可用于SSL连接启用的名字@Overridepublic String[] getSupportedCipherSuites() {return delegate.getSupportedCipherSuites();}@Overridepublic Socket createSocket(final Socket socket, final String host, final int port,final boolean autoClose) throws IOException {final Socket underlyingSocket = delegate.createSocket(socket, host, port, autoClose);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final String host, final int port) throws IOException {final Socket underlyingSocket = delegate.createSocket(host, port);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final String host, final int port, final InetAddress localAddress,final int localPort) throwsIOException {final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final InetAddress host, final int port) throws IOException {final Socket underlyingSocket = delegate.createSocket(host, port);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress,final int localPort) throwsIOException {final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);return overrideProtocol(underlyingSocket);}private Socket overrideProtocol(final Socket socket) {if (!(socket instanceof SSLSocket)) {throw new RuntimeException("An instance of SSLSocket is expected");}((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1"});return socket;}}}

如果在调用的时候出现了javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)异常 需要配置JM的参数设置,经过百度查询

原因: 配置文件中禁用 TLSv1, TLSv1.1

修改如下:

把..\Java\jdk-11.0.11\conf\security文件下的java.security文件

将 :

jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
     DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
     include jdk.disabled.namedCurves

替换为:

jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, \
     DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
     include jdk.disabled.namedCurves

测试post:

企业微信上传临时素材文件相关推荐

  1. 企业微信 上传临时素材 JAVA

    上传临时素材 Controller @ResponseBody@RequestMapping(value = "/uploadFile", method = RequestMeth ...

  2. .net core 使用HttpClient为企业微信上传临时素材

    背景: 企微的图片三天有效,所以需要一个定时程序把快到期的图片又传一次到企微上面去 /// <summary>/// 向企业微信临时素材提交数据/// </summary>// ...

  3. Java 微信上传临时素材

    微信小程序创建直播间,服务端上传临时素材,微信开放社区隐藏太深,不好找,记录一下,抄就行,亲测好用!!! /** * graphurl:上传临时素材微信接口地址,自己拼就行,file:客户端上传的文件 ...

  4. Node使用微信上传临时素材接口

    引言 在开发小程序后端的时候,遇到需求,需要接受小程序客服信息用户回复的特定指端,返回对应的图片,然而图片是存在oss或者是某些特殊链接转成base64的形式,在使用微信提供的临时素材上传接口的时候, ...

  5. C#-微信公众平台接口-上传临时素材

    最烦做微信公众平台的东西..文档说得不清不楚,又没示例代码,只能自己 慢慢搜索,弄了一晚上,基本弄出来了,把本地的图片上传到微信的临时素材那里,返回媒体ID,用于其他操作,代码如下 :(自己导入相应的 ...

  6. Java实现企业微信上传临时文件获取media_id

    目录 1 上传临时素材API 2 测试代码 3 测试完成 4 遇到的问题 4.1 文件类型无法解析 1 上传临时素材API 调试工具 素材上传得到media_id,该media_id仅三天内有效 me ...

  7. 微信上传图文素材接口报41005错误解决方法

    微信上传图文素材接口报41005错误解决方法 参考文章: (1)微信上传图文素材接口报41005错误解决方法 (2)https://www.cnblogs.com/gy1010/p/6674529.h ...

  8. PHP对接企业微信API上传临时素材

    因为项目需求, 需要把企业微信上面的审批搬到公司的后台, 需要对接企业微信API的审批, 里面有一个功能是上传附件, 可是我看了, 文档写的不是很清楚, 什么form-data什么的, 看的我云里雾里 ...

  9. 企业微信上传文件到服务器,上传素材到腾讯企业微信服务器

    //filetype:文件类型:filePath:文件所在路径 @Override public JSONObject uploadMedia(String fileType, String file ...

最新文章

  1. 使用回调函数实现图像阈值分析。程序运行后在屏幕中输入阈值,通过改变滑动条实现不同类型的二值化图。
  2. 20强名单公布!2021 OceanBase 数据库大赛决赛酣战在即!
  3. ICLR 2020 | ELECTRA:新型文本预训练模型
  4. OPKG 软件包管理
  5. zookeeper动物园管理员学习笔记
  6. Hadoop何以快速成为最佳网络安全工具?
  7. VB6:通过ADO访问Oracle存储过程返回的结果集
  8. 第一次使用 Blog
  9. C++中友元函数,友元类数详解
  10. 加强计算机网络应用,如何加强计算机网络管理技术创新应用
  11. html如何在网页上看错误,HTML错误时,Spring MVC的,但不能查看网页时,静态
  12. 大中小型项目管理的区别
  13. CTWing-中国电信IoT物联网平台设备接入实战
  14. 网上商城系统支付方式如何配置?支付方式有哪些
  15. iPhone加码“独立王国” 有可能成摆设?
  16. 【计算机图形学】Bezier曲线软件及操作
  17. 基于QPSK+LDPC的微波信道误码率matlab仿真
  18. python打包加密工具:Pyinstaller和Nuitka
  19. 微信小程序 宠物社区源码
  20. memwatch使用笔记

热门文章

  1. 如何查看本地ip地址和外网地址
  2. 保证只要看一遍,新手也能写出来的超简单五子棋代码
  3. 中药图片拍照识别系统全套开源
  4. Java练习习题,百钱买百鸡问题,用100文钱买鸡,公鸡5文钱一只,母鸡3文钱一只,小鸡3只1文钱
  5. PC 网易云音乐桌面歌词原版天际蓝配色方案
  6. 2021cka考试标准答案
  7. import image的坑
  8. Paper翻译:《A Novel Convolutional Neural Network Based Model for Recognition and Classification of App》
  9. jPBC 2.0.0配置与测试(补充版)
  10. ES6: 支持ES6的浏览器版本(汇总表)