imgSecCheck校驗一張圖片是否含有違法違規內容html
我的小程序只是圖片的一些處理識別。固只拿imgSecCheck接口進行代碼示例了。
1.在本身的Java後臺服務增長接口調用(api.weixin.qq.com不能直接加入小程序安全域名中)
2.在小程序選擇圖片後優先走違法違規校驗。校驗經過再走本身的業務代碼。java
wx.uploadFile({ url: 圖片違法違規校驗接口地址, filePath: res.tempFilePaths[0], header: { 'content-type': 'multipart/form-data' }, name: 'file', success: function(checkres) { var checkResult = JSON.parse(checkres.data); console.info(checkResult); if (checkResult.errcode == '0') { //校驗沒有違法違規進行本身業務代碼處理 } else { if (checkResult.errcode == '87014') { wx.hideLoading(); wx.showModal({ content: '存在敏感內容,請更換圖片', showCancel: false, confirmText: '明白了' }) } else { wx.hideLoading(); wx.showModal({ content: '其餘錯誤,稍後再試', showCancel: false, confirmText: '明白了' }) } } }
AccessTokenWX .java小程序
import com.fasterxml.jackson.annotation.JsonInclude; /** * @Description AccessTokenWX * @author 小帥丶 * @className AccessTokenWX * @Date 2019/9/29-10:22 **/ @JsonInclude(JsonInclude.Include.NON_NULL) public class AccessTokenWX { private String access_token; private Integer expires_in; private String errcode; private String errmsg; //set/get省略 }
/** * 圖片過濾檢測 * @param file 圖片文件 * @return */ @RequestMapping(value = "/imgcheck", method = {RequestMethod.POST}) @ResponseBody public AccessTokenWX checkPic(@RequestParam(value = "file") MultipartFile file, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { String token = ""; //本身寫一個定時任務或其餘方式 獲取AccessToken AccessTokenWX accessTokenWX = new AccessTokenWX(); try { token = getAccessTokenJob.getAccessToken(); String url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" + token; String result = uploadFile(url, file); accessTokenWX = JSON.parseObject(result, AccessTokenWX.class); System.out.println("圖片檢測結果 = " + result); return accessTokenWX; } catch (Exception e) { System.out.println("----------------調用騰訊內容過濾系統出錯------------------" + e.getMessage()); accessTokenWX.setErrcode("500"); accessTokenWX.setErrmsg("system錯誤"); return accessTokenWX; } } /** * 上傳二進制文件 * @param graphurl 接口地址 * @param file 圖片文件 * @return */ public static String uploadFile(String graphurl,MultipartFile file) { String line = null;//接口返回的結果 try { // 換行符 final String newLine = "\r\n"; final String boundaryPrefix = "--"; // 定義數據分隔線 String BOUNDARY = "========7d4a6d158c9"; // 服務器的域名 URL url = new URL(graphurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 設置爲POST情 conn.setRequestMethod("POST"); // 發送POST請求必須設置以下兩行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 設置請求頭參數 conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY); conn.setRequestProperty("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"); OutputStream out = new DataOutputStream(conn.getOutputStream()); // 上傳文件 StringBuilder sb = new StringBuilder(); sb.append(boundaryPrefix); sb.append(BOUNDARY); sb.append(newLine); // 文件參數,photo參數名能夠隨意修改 sb.append("Content-Disposition: form-data;name=\"image\";filename=\"" + "https://api.weixin.qq.com" + "\"" + newLine); sb.append("Content-Type:application/octet-stream"); // 參數頭設置完之後須要兩個換行,而後纔是參數內容 sb.append(newLine); sb.append(newLine); // 將參數頭的數據寫入到輸出流中 out.write(sb.toString().getBytes()); // 讀取文件數據 out.write(file.getBytes()); // 最後添加換行 out.write(newLine.getBytes()); // 定義最後數據分隔線,即--加上BOUNDARY再加上--。 byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine).getBytes(); // 寫上結尾標識 out.write(end_data); out.flush(); out.close(); // 定義BufferedReader輸入流來讀取URL的響應 BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); while ((line = reader.readLine()) != null) { return line; } } catch (Exception e) { System.out.println("發送POST請求出現異常!" + e); } return line; }
access_token 接口調用憑證 file 圖片文件
/** * 圖片過濾檢測 * @param file 要校驗的圖片 * @param access_token 接口調用憑證 * @return */ @RequestMapping(value = "/imgcheck/general", method = {RequestMethod.POST}) @ResponseBody public AccessTokenWX checkPicgeneral(@RequestParam(value = "file") MultipartFile file, String access_token,HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { logger.info("獲取access_token======" + access_token + "訪問的ip" + httpServletRequest.getRemoteAddr()); AccessTokenWX accessTokenWX = new AccessTokenWX(); try { String url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=" + access_token; String result = uploadFile(url, file); accessTokenWX = JSON.parseObject(result, AccessTokenWX.class); System.out.println("圖片檢測結果 = " + result); return accessTokenWX; } catch (Exception e) { System.out.println("----------------調用騰訊內容過濾系統出錯------------------" + e.getMessage()); accessTokenWX.setErrcode("500"); accessTokenWX.setErrmsg("system錯誤"); return accessTokenWX; } }