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();
}
Thank you very much.
– leeosnt
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?– Renan Gomes
@rrnan The URL that is accessed requires authentication, and the OP has a key. About
ImageIO.write
that method requires an image format.– stderr