简体   繁体   中英

Sending base64 image via http request in Java miss up some chars

I've been trying to send a base64 image using java to NodeJS API, after working and searching for hours I could not know what might cause the following problem, the problem as following:

Here's a part of the original base64 in Java

f8A0NH2qH+/+hooouAfaof7/wCho+1Q/

and here's is a part of the received image in NodeJS

f8A0NH2qH / hooouAfaof7/wCho 1Q/

I've tried to send an image via POSTMAN and no problem at all.

All steps as following:

1- I am converting an image to base64 using the following snippet

public static String imgToBase64String(final RenderedImage img, final String formatName) {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(img, formatName, Base64.getEncoder().wrap(os));
            return os.toString(StandardCharsets.ISO_8859_1.name());
        } catch (final IOException ioe) {
            throw new UncheckedIOException(ioe);
        }
    }

    public static BufferedImage base64StringToImg(final String base64String) {
        try {
            return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64String)));
        } catch (final IOException ioe) {
            throw new UncheckedIOException(ioe);
        }
    }

And to take screenshot

    final Robot robot = new Robot();
    final Rectangle r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    final BufferedImage bi = robot.createScreenCapture(r);
    final String base64String = Base64Converter.imgToBase64String(bi, "jpg");

2- I am using Gson library to stringify object

3- I am using bodyParser in NodeJS

4- Sending HTTP request as:

public static void sendPOST(String image) throws Exception {
        String POST_PARAMS = "screenShotData";
        URL obj = new URL(POST_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setConnectTimeout(5000); // 5 seconds
        con.setReadTimeout(5000); // 5 seconds

        Gson gson = new Gson();
        Http.ScreenShot screenShot = new ScreenShot(); // This is just a class with a string property
        screenShot.setImage(image);
        POST_PARAMS += gson.toJsonTree(screenShot).getAsJsonObject();


        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        byte[] outputBytesArray = POST_PARAMS.getBytes();
        os.write(outputBytesArray);
        os.flush();
        os.close();


        int responseCode = con.getResponseCode();
        System.out.println("POST Response Code :: " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            Object responseObject = gson.fromJson(response.toString(), Object.class);
            System.out.println("Res: " + responseObject);
        } else {
            System.out.println(con.getResponseMessage());
        }
    }

In URL encoded text, the + character means a space character. For example,

https://example.com/?s=nodejs+bodyparser

sends the s parameter with the value

nodejs bodyparser 

(notice the space).

When you do an ordinary form post (the kind browsers do) you use the application/x-www-form-urlencoded data type, meaning the payload of your POST operation looks like a query string. I think you are passing a JSON object as a text string without url-encoding it.

You probably want to use the application/json data type instead. nodejs's body parser detects, from your Content-type header, that it's JSON and parses it correctly.

Try this. (not debugged, sorry.)

    string payload = gson.toJsonTree(screenShot).getAsJsonObject();
    byte[] outputBytesArray = payload.getBytes();

    con.setRequestProperty("Content-Type", "application/json");
    con.setDoOutput(true);
    OutputStream os = con.getOutputStream();
    os.write(outputBytesArray);
    os.flush();
    os.close();

You forgot to close the wrapped Base64 encoder stream. Only if you close it the end of the base64 encoded data can be written:

public static String imgToBase64String(final RenderedImage img, final String formatName) {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        try (OutputStream wrapped = Base64.getEncoder().wrap(os)) {
            ImageIO.write(img, formatName, wrapped);
        }
        return os.toString(StandardCharsets.ISO_8859_1.name());
    } catch (final IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}

I had faced this issue, but I had no authority to change in class or something else. I had just simply replaced space with +, in most of the cases it was working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM