How to send a BufferedImage by POST method?

1

Well, I wanted to know how to send an image of type BufferedImage to a link using POST method.

I'm using this code to send, but I'm not sure 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();
    }
    
asked by anonymous 18.01.2015 / 20:24

1 answer

1

The method below does what you ask for, code extracted from the SOen response .

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();
    }
}
    
18.01.2015 / 20:32