微信小程序校验上传内容图片合法性,JAVA后端HTTPS调用实现
package org
.jeecg
.common
.wxhttp
;
import java
.io
.IOException
;
import java
.util
.HashMap
;
import java
.util
.Iterator
;
import java
.util
.Map
;
import java
.util
.Map
.Entry
;
import org
.apache
.commons
.lang3
.StringUtils
;
import org
.jeecg
.common
.util
.RedisUtil
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.http
.HttpEntity
;
import org
.springframework
.http
.HttpHeaders
;
import org
.springframework
.http
.HttpMethod
;
import org
.springframework
.http
.MediaType
;
import org
.springframework
.http
.ResponseEntity
;
import org
.springframework
.stereotype
.Service
;
import org
.springframework
.util
.LinkedMultiValueMap
;
import org
.springframework
.util
.MultiValueMap
;
import org
.springframework
.web
.client
.RestTemplate
;
import org
.springframework
.web
.multipart
.MultipartFile
;
import com
.alibaba
.fastjson
.JSONObject
;
@Service
public class RestTemplateToInterface {
@Autowired
private RestTemplate restTemplate
;
private static final String
RESPONSE_SUCCEED="0";
private static final String
WX_API_TOKEN="WX_API_TOKEN";
private static final long
TOKEN_TIME_OUT_SECOND=60*60*1;
@Autowired
private TokenReq req
;
@Autowired
private RedisUtil redisUtil
;
public String
getWxToken(){
String token
= (String
) redisUtil
.get(WX_API_TOKEN);
if(StringUtils
.isNotBlank((String
) token
)){
return token
;
}else{
String url
="https://api.weixin.qq.com/cgi-bin/token";
StringBuffer stringBuffer
=new StringBuffer(url
);
Map
<String
,String
> map
=new HashMap<>();
map
.put("grant_type", req
.getGrant_type());
map
.put("appid", req
.getAppid());
map
.put("secret", req
.getSecret());
Iterator
<Entry
<String
, String
>> iterator
= map
.entrySet().iterator();
if (iterator
.hasNext()) {
stringBuffer
.append("?");
while (iterator
.hasNext()) {
Map
.Entry
<String
, String
> entry
= iterator
.next();
if (entry
.getValue() != null) {
stringBuffer
.append(entry
).append("&");
}
url
= stringBuffer
.substring(0, stringBuffer
.length() - 1);
}
}
GeneralToken responseEntity
= restTemplate
.getForObject(url
, GeneralToken
.class);
token
= responseEntity
.getAccess_token();
redisUtil
.set(WX_API_TOKEN, token
, TOKEN_TIME_OUT_SECOND);
}
return token
;
}
public boolean
toCheckText(String content
,String token
){
String url
="https://api.weixin.qq.com/wxa/msg_sec_check?access_token="+token
;
HttpHeaders headers
= new HttpHeaders();
headers
.setContentType(MediaType
.APPLICATION_JSON);
JSONObject obj
= new JSONObject();
obj
.put("content", content
);
HttpEntity
<JSONObject
> request
= new HttpEntity<>(obj
, headers
);
ResponseEntity
<JSONObject
> responseEntity
= restTemplate
.exchange(url
, HttpMethod
.POST, request
, JSONObject
.class);
JSONObject json
= responseEntity
.getBody();
if(RESPONSE_SUCCEED.equals(json
.getString("errcode"))){
return true;
}
return false;
}
public boolean
toCheckImg(MultipartFile file
,String token
) throws IOException
{
String url
="https://api.weixin.qq.com/wxa/img_sec_check?access_token="+token
;
HttpHeaders headers
= new HttpHeaders();
MediaType type
= MediaType
.parseMediaType("multipart/form-data");
headers
.setContentType(type
);
MultiValueMap
<String
, Object
> map
= new LinkedMultiValueMap<>();
map
.add("media", file
.getResource());
HttpEntity
<MultiValueMap
<String
, Object
>> request
= new HttpEntity<MultiValueMap
<String
, Object
>>(map
, headers
);
JSONObject json
= restTemplate
.postForObject(url
, request
, JSONObject
.class);
if(RESPONSE_SUCCEED.equals(json
.getString("errcode"))){
return true;
}
return false;
}
}
使用springboot自带的HTTP调用请模板
package org.jeecg.common.wxhttp;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* HTTP 请求 调用第三方服务
* @Author: songcheng
* @Date: 2020/06/28
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}
Pom文件 引入
<!--spring restTemplate-->
<!-- @ConfigurationProperties annotation processing (metadata for IDEs)
生成spring-configuration-metadata.json类,需要引入此类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
```