Skip to content
Advertisement

Getting image in POST request using Java and vert.x

I got the following question, I can’t get image which I’m sending from android app to server, I’m sending image from app using kotlin, and receiving it on server written with Java using vert.x.

I send it with following code:

val serverUrl = "http://192.168.56.1:8080/recognize/get_text_from_image"

    val client = OkHttpClient.Builder()
        .build()

    val stream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
    val byteArray = stream.toByteArray()
    val encodedImage: String = encodeToString(byteArray, DEFAULT)

    val requestBody: RequestBody = RequestBody.create(MediaType.parse("image/*jpg"), encodedImage)

    val request: Request = Request.Builder()
        .url(serverUrl)
        .post(requestBody)
        .build()

on server side I’m handling it like

    Router baseRouter = Router.router(vertx);
        Router apiRouter = Router.router(vertx);

        baseRouter.mountSubRouter("/recognize", apiRouter);

        vertx.createHttpServer()
                .requestHandler(baseRouter::accept)
                .listen(8080, result -> {
                    if (result.succeeded()) {
                        System.out.println("completed");
                        startFuture.complete();
                    } else {
                        startFuture.fail(result.cause());
                        System.out.println(result.cause().getLocalizedMessage());
                    }
                });

        apiRouter.route(HttpMethod.POST, "/get_text_from_image").handler(rc -> {

            BufferedImage bufferedImage = null;
            byte[] imageByte;
            try {
                BASE64Decoder decoder = new BASE64Decoder();
                imageByte = decoder.decodeBuffer(rc.getBodyAsString());
                ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
                bufferedImage = ImageIO.read(bis);
                bis.close();
                System.out.println("Decoding bytes to image");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Error while decoding bytes to image");
            }
            File outputfile = new File("/home/user/image.jpg");
            try {
                ImageIO.write(bufferedImage, "jpg", outputfile);
                System.out.println("Saving image");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("Error while saving image");
            }

            JsonObject json = rc.getBodyAsJson();
            System.out.println(json.getString("id"));
            HttpServerResponse response = rc.response();
            response.putHeader("content-type", "application/json");

            // Write to the response and end it
            response.end("{"status": 200}");
        });
}

But I can’t get anything into imageByte variable.

Advertisement

Answer

You need to add BodyHandler before all routes

Router baseRouter = Router.router(vertx);
baseRouter.route().handler(BodyHandler.create());

more info here https://vertx.io/preview/docs/apidocs/io/vertx/ext/web/handler/BodyHandler.html

Advertisement