java--百度圖片稽核 image format error解決
阿新 • • 發佈:2020-12-28
影象稽核API參考
文件地址
https://cloud.baidu.com/doc/ANTIPORN/s/jk42xep4e
注意:
Content-Type為application/x-www-form-urlencoded
,然後通過urlencode
格式化請求體。
程式碼
1.圖片轉base64
注意使用java.util.Base64的包,生成的base64才不會帶換行符
import java.util.Base64; public static String convertFileToBase64(String imgPath) { byte[] data = null; // 讀取圖片位元組陣列 try { InputStream in = new FileInputStream(imgPath); System.out.println("檔案大小(位元組Byte)=" + in.available()); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對位元組陣列進行Base64編碼,得到Base64編碼的字串 return new String(Base64.getEncoder().encodeToString(data)); }
2.傳送POST請求
/** * 向指定URL傳送POST方法的請求 Content-Type=application/x-www-form-urlencoded * * @param targetUrl 傳送請求的URL * @param params 請求引數,請求引數應該是name1=value1&name2=value2的形式。 * @return JSONObject 返回的JSON資料 */ public static JSONObject postFormUrlEncoded(String targetUrl, String params) { HttpURLConnection urlConnection = null; try { URL url = new URL(targetUrl.trim()); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); // 百度要求此型別上傳圖片 urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setUseCaches(false); urlConnection.connect(); PrintWriter out = new PrintWriter(new OutputStreamWriter(urlConnection.getOutputStream(), StandardCharsets.UTF_8)); // 寫入引數 out.print(params); out.flush(); int resultCode = urlConnection.getResponseCode(); if (HttpURLConnection.HTTP_OK == resultCode) { StringBuffer stringBuffer = new StringBuffer(); String readLine; BufferedReader responseReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8)); while ((readLine = responseReader.readLine()) != null) { stringBuffer.append(readLine); } responseReader.close(); return JSONObject.parseObject(stringBuffer.toString()); } out.close(); } catch (Exception e) { return null; } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return null; }
3.圖片稽核
/** * 獲得校驗結果 * * @param imgBase64 Base64格式 * @param imgType 0 靜態圖片 1 動態圖片 * @return * @throws UnsupportedEncodingException */ public static Boolean getCensorResult(String imgBase64, String imgType) { if (ValidatorUtils.isEmpty(accessToken)) { getAccessToken(); } try { String params = "image=" + URLEncoder.encode(imgBase64,"UTF-8") + "&imgType=" + imgType + "&access_token=" + accessToken; JSONObject jsonObject; //post 請求方式 jsonObject = HttpUtils.postFormUrlEncoded(host, params); if (jsonObject.get("error_code") != null) { // token失效 if ("110".equals(jsonObject.get("error_code"))) { getAccessToken(); } System.out.println(" 校驗失敗,原因:" + jsonObject.get("error_msg")); return false; } if ("1".equals(jsonObject.get("conclusionType").toString())) { return true; } else { System.out.println(" 校驗失敗,原因:" + jsonObject.get("data")); return false; } }catch (UnsupportedEncodingException e){ e.printStackTrace(); return false; } }
踩坑
通過第一段程式碼的返回的base64圖片格式需要使用URLEncoder格式化一遍,否則會報錯
String params = "image=" + URLEncoder.encode(imgBase64,"UTF-8") + "&imgType=" + imgType + "&access_token=" + accessToken;
錯誤的寫法
String params = "image=" + imgBase64 + "&imgType=" + imgType + "&access_token=" + accessToken;