How to send a Bufferedimage by POST method?

Asked

Viewed 188 times

1

Well, I was wondering how do I send a picture of the guy BufferedImage by a link using method POST.

I’m using this code to send, but I don’t know how to send this BufferedImage.

 public static String getImgurContent() throws Exception {
        URL url;
        url = new URL("https://api.imgur.com/3/image");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        BufferedImage img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode("BUFFERED_IMAGE_AQUI", "UTF-8");

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Authorization", "Client-ID " + "00000000000");
        conn.setRequestMethod("POST");

        conn.connect();
        StringBuilder stb = new StringBuilder();
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();


        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            stb.append(line).append("\n");
        }
        wr.close();
        rd.close();

        return stb.toString();
    }

1 answer

1


The method below does what you ask, code extracted from this reply soen.

public static void upload(BufferedImage image) {
    String IMGUR_POST_URI = "https://api.imgur.com/3/upload";
    String IMGUR_API_KEY = "00000000000";

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        System.out.println("Writing image...");
        ImageIO.write(image, "png", baos);
        URL url = new URL(IMGUR_POST_URI);

        System.out.println("Encoding...");
        String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
        data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(IMGUR_API_KEY, "UTF-8");

        System.out.println("Connecting...");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        System.out.println("Sending data...");
        wr.write(data);
        wr.flush();

        System.out.println("Finished.");

        //just display the raw response
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();

    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
        e.printStackTrace();
    }
}
  • Thank you very much.

  • I don’t know where the author of the code took that parameter "key" containing as value the client id. From what I saw in the API to send photos without authentication (as in this case) just the Client-ID in the header, for them to know which application is sending the photo. Another point is that all images, regardless of the extension, will be converted to ". png" (ImageIO.write(image, "png", baos);). It works?

  • 1

    @rrnan The URL that is accessed requires authentication, and the OP has a key. About ImageIO.write that method requires an image format.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.