直接上代码
package com.ink.manager.controller.weixin;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import .HttpURLConnection;import .URL;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.entity.ContentType;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.springframework.web.client.RestTemplate;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;/*** 微信小程序* 校验文字/图片是否含有违法违规内容* @author Administrator*/public class ChickContent {/*** 检查图片是否含有违法违规内容* @param inputStream* @param contentType* @return* @throws Exception*/public static Boolean checkPic(InputStream inputStream, String contentType) throws Exception{//获取ACCESS_TOKENString accessToken = postToken();CloseableHttpClient httpclient = HttpClients.createDefault();CloseableHttpResponse response = null;HttpPost request = new HttpPost("https://api./wxa/img_sec_check?access_token=" + accessToken);request.addHeader("Content-Type", "application/octet-stream");byte[] byt = new byte[inputStream.available()];inputStream.read(byt);request.setEntity(new ByteArrayEntity(byt, ContentType.create(contentType)));response = httpclient.execute(request);HttpEntity httpEntity = response.getEntity();String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成stringJSONObject jso = JSONObject.parseObject(result);return getResult(jso);}/*** 检查一段文本是否含有违法违规内容* @param content* @return* @throws Exception*/public static Boolean weixin_content(String content) throws Exception{//获取ACCESS_TOKENString accessToken = postToken();System.out.println(accessToken);//data数据JSONObject data = new JSONObject();data.put("content",content);RestTemplate restTemplate = new RestTemplate();String url = ("https://api./wxa/msg_sec_check?access_token="+accessToken);String response = restTemplate.postForObject(url, data, String.class);JSONObject jso = JSONObject.parseObject(response);return getResult(jso);}/*** 校验* @param result* @return*/private static Boolean getResult(JSONObject result){Object errcode = result.get("errcode");int errCode = (Integer)errcode;if (errCode == 0) {return true;} else if (errCode != 0) {return false;}return true;}/*** 用于获取access_token* @param params* @param APIKEY 小程序id* @param SECRETKEY小程序密钥* @return access_token* @throws Exception*/public static String postToken() throws Exception {String APIKEY = "xxxxxxxxx";//小程序idString SECRETKEY = "xxxxxxxxxx";//小程序密钥String requestUrl = "https://api./cgi-bin/token?grant_type=client_credential&appid="+APIKEY+"&secret="+SECRETKEY;URL url = new URL(requestUrl);// 打开和URL之间的连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");// 设置通用的请求属性connection.setRequestProperty("Content-Type", "application/json");connection.setRequestProperty("Connection", "Keep-Alive");connection.setUseCaches(false);connection.setDoOutput(true);connection.setDoInput(true);// 得到请求的输出流对象DataOutputStream out = new DataOutputStream(connection.getOutputStream());out.writeBytes("");out.flush();out.close();// 建立实际的连接connection.connect();// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = null;if (requestUrl.contains("nlp"))in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));elsein = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String result = "";String getLine;while ((getLine = in.readLine()) != null) {result += getLine;}in.close();com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(result);String accesstoken=jsonObject.getString("access_token");return accesstoken;} }
inputStream/contentType这两个参数,可以通过FileItem/MultipartFile之类的来获取
如:
List<FileItem> list = servletFileUpload.parseRequest(request);for(FileItem item : list){Boolean flag = ChickContent.checkPic(item.getInputStream(),item.getContentType());if(flag== false){System.out.println("上传图片-----结束:该图片含有违法违规内容");}}@PostMapping("/check_image")public Boolean checkImage(@RequestPart(value = "file") MultipartFile multipartFile){Boolean flag = ChickContent.checkPic(multipartFile.getInputStream(),multipartFile.getContentType());...}
如果觉得《微信小程序发布时需要校验违法违规内容 图片 Java后端接口》对你有帮助,请点赞、收藏,并留下你的观点哦!