package cn.yuc.cloud.rest.common;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; 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.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.*;
@Slf4j public class SenInfoCheckUtil { /** * 图片违规检测 * @param accessToken * @param file * @return */ public static Boolean imgFilter(String accessToken, InputStream inputStream){ return checkPic(inputStream, accessToken); }
/** * 文本违规检测 * @param accessToken * @param content * @return */ public static Boolean cotentFilter(String accessToken, String content){ return checkContent(accessToken,content); }
/** * 恶意图片过滤 * @param inputStream * @return */ private static Boolean checkPic(InputStream inputStream, String accessToken) { try {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpPost request = new HttpPost("https://api.weixin.qq.com/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("image/jpg")));
response = httpclient.execute(request); HttpEntity httpEntity = response.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); log.info("----------------调用腾讯内容过滤系统出错------------------"); return true; } }
/** * 图片违规检测,对外提供,直接使用 * * @param accessToken * @param MultipartFile * @return */ public static Boolean imgFilterr(String accessToken, MultipartFile file) { String contentType = file.getContentType(); return checkPicc(file, accessToken, contentType); }
private static Boolean checkPicc(MultipartFile multipartFile, String accessToken, String contentType) { try { CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; HttpPost request = new HttpPost("https://api.weixin.qq.com/wxa/img_sec_check?access_token=" + accessToken); request.addHeader("Content-Type", "application/octet-stream"); InputStream inputStream = multipartFile.getInputStream(); 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");// 转成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); return true; } } private static Boolean checkContent(String accessToken,String content) { try { CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpPost request = new HttpPost("https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken); request.addHeader("Content-Type", "application/json"); Map<String, String> map = new HashMap<>(); map.put("content",content); String body = JSONObject.toJSONString(map); request.setEntity(new StringEntity(body,ContentType.create("text/json", "UTF-8"))); response = httpclient.execute(request); HttpEntity httpEntity = response.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); log.info("----------------调用腾讯内容过滤系统出错------------------"); return true; } }
private static Boolean getResult(JSONObject jso){ Object errcode = jso.get("errcode"); int errCode = (int) errcode; if (errCode == 0) { return true; } else if (errCode == 87014) { log.info("图片内容违规-----------"); return false; } return true; } public static void main(String[] args) throws Exception { String token = "123"; //Boolean b = checkContent(token,"大大"); //InputStream inputStream = returnBitMap("http://img-yuc.feifanxinli.com/local/yuc/web/img/159356901274086.png"); MultipartFile m = createFileItem("http://img-yuc.feifanxinli.com/local/yuc/web/img/159356779146083.png","image"); Boolean b = imgFilterr(token, m); System.out.println(b); } public static InputStream returnBitMap(String path) { URL url = null; InputStream is =null; try { url = new URL(path); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) url.openConnection();//利用HttpURLConnection对象,我们可以从网络中获取网页数据. conn.setDoInput(true); conn.connect(); is = conn.getInputStream(); //得到网络返回的输入流
} catch (IOException e) { e.printStackTrace(); } return is; } /** * url转变为 MultipartFile对象 * @param url * @param fileName * @return * @throws Exception */ public static MultipartFile createFileItem(String url, String fileName) throws Exception{ FileItem item = null; try { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setReadTimeout(30000); conn.setConnectTimeout(30000); //设置应用程序要从网络连接读取数据 conn.setDoInput(true); conn.setRequestMethod("GET"); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream is = conn.getInputStream();
FileItemFactory factory = new DiskFileItemFactory(16, null); String textFieldName = "uploadfile"; item = factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName); OutputStream os = item.getOutputStream();
int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = is.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); is.close(); } } catch (IOException e) { throw new RuntimeException("文件下载失败", e); }
return new CommonsMultipartFile(item); } }