1

我一直在努力做到这一点,并且可能阅读了一百篇关于它的帖子,但是,我仍然没有做对,恐怕我在做一些非常愚蠢的事情。(使用 apache commons 编解码器库)

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

好的,这是我的输出:

    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)

我不确定问题是什么,我怀疑它甚至没有上传,即使没有抛出错误

4

1 回答 1

0

试试 -->

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();
    }
}
于 2014-10-27T09:04:26.023 回答