1

I have been trying to get this right and probably read a hundred SO posts about it, however, I still haven't got it right and am afraid i'm doing something really stupid. (using the apache commons codec library)

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.*;
    import javax.imageio.ImageIO;
    import org.apache.commons.codec.binary.Base64;

    public class UploadTest
    {
        public static void main (String[] args)
        {
            try
            {
                File pic = new File ("C:\\pic.jpg");
                upload ( (BufferedImage) ImageIO.read (pic)   );
            }
            catch (Exception e)
            {
                System.out.println("Error: " + e.getMessage() );
                e.printStackTrace();
            }
        }

        public static void upload(BufferedImage image)
        {
            String IMGUR_POST_URI = "http://api.imgur.com/2/upload.xml";
            String IMGUR_API_KEY = "my_api_key";

            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);
                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();
            }
        }
    }

Ok so this is my output:

    Writing image...
    Encoding...
    Connecting...
    Sending data...
    Finished.
    Error: Server returned HTTP response code: 400 for URL:     http://imgur.com/api/upload.xml
    java.io.IOException: Server returned HTTP response code: 400 for URL: http://imgur.com/api/upload.xml
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
    at UploadTest.upload(UploadTest.java:52)
at UploadTest.main(UploadTest.java:15)

I'm not sure what the problem is and I have doubts that its not even uploading, even though no errors are being thrown

2
  • ok so whats the error or the question Commented Sep 20, 2013 at 18:32
  • @redFIVE apologies, premature submit.
    – SimpleJack
    Commented Sep 20, 2013 at 18:36

1 Answer 1

0

Try -->

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

    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();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.