Java Google Cloud Vision API-在POST请求中发送base64编码图像
我正在用谷歌视觉API做一个小项目。我想检测在POST请求中发送给API的base64编码图像的正面。我的代码基于以下谷歌教程:。显然,可以发送base64编码的图像 这是我的密码:Java Google Cloud Vision API-在POST请求中发送base64编码图像,java,maven,http,post,google-cloud-vision,Java,Maven,Http,Post,Google Cloud Vision,我正在用谷歌视觉API做一个小项目。我想检测在POST请求中发送给API的base64编码图像的正面。我的代码基于以下谷歌教程:。显然,可以发送base64编码的图像 这是我的密码: File File = new File(args[2]); String ImageString; FileInputStream fileInputStreamReader = new FileInputStream(File); byte[] bytes = new byte
File File = new File(args[2]);
String ImageString;
FileInputStream fileInputStreamReader = new FileInputStream(File);
byte[] bytes = new byte[(int)File.length()];
fileInputStreamReader.read(bytes);
ImageString = Base64.getEncoder().encodeToString(bytes);
URL serverUrl = new URL(TARGET_URL + API_KEY); //TARGET_URL = "https://vision.googleapis.com/v1/images:annotate?"
URLConnection urlConnection = serverUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)urlConnection;
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
httpConnection.setDoOutput(true);
BufferedWriter httpRequestBodyWriter = new BufferedWriter(new
OutputStreamWriter(httpConnection.getOutputStream()));
httpRequestBodyWriter.write
("{\"requests\": [{ \"features\": [ {\"type\": \"FACE_DETECTION\""
+"}], \"image\": {\"content\": \"" + ImageString + "\"}]}");
httpRequestBodyWriter.close();
如您所见,我用字符串替换了“content”字段。我只是不知道我做错了什么。
提前谢谢 你犯了什么错误?看起来你的大括号不平衡,所以你的内容不是一个有效的JSON,它不应该以}}}]}结尾吗?我有一个错误的请求,我忘记了一个“}”。我以为我用JSONLint验证了JSON。现在可以了!非常感谢。