服务器对包含base64图像的java http发布请求做出“错误:无效图像”响应

时间:2019-02-19 10:49:07

标签: java http post base64

我正在设置一个用Java编写的客户端,该客户端应该将2个base64编码的图像发送到服务器。服务器以“错误:无效图像”响应 除此之外,Post请求应该是正确制定的​​。 我在这里想念什么?

这是我正在使用的Web API的官方文档: https://documenter.getpostman.com/view/5211511/RWaDYCUN#49003cb5-55d7-d363-bb64-d202fd8d9cd7 我专门使用了比较面孔端点。

this.address包含端点的正确Url和变量 image_camera和image_user包含BufferedImage类型的实例。

public String compare() {

    String form1_64, form2_64;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Base64.Encoder encoder = Base64.getEncoder();
    try {
        String form1_net = "data:image/png;base64";
        String form2_net = "data:image/png;base64";
        // TODO invalid image file in FaceX response
        ImageIO.write(image_camera, "png", bos);
        form1_64 = encoder.encodeToString(bos.toByteArray());
        ImageIO.write(image_user, "png", bos);
        form2_64 = encoder.encodeToString(bos.toByteArray());
        form1_net += form1_64;
        form2_net += form2_64;

        String url = String.valueOf(this.address);
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);

        List<NameValuePair> urlParams = new ArrayList<>();
        urlParams.add(new BasicNameValuePair("image_attr", form1_net));
        urlParams.add(new BasicNameValuePair("image_attr", form2_net));

        post.setHeader("user_id", "my-user-id");
        post.setHeader("user_key", "my-user-key");

        post.setEntity(new UrlEncodedFormEntity(urlParams));

        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        System.out.println(result.toString());

    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

0 个答案:

没有答案