Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String encodeToString(BufferedImage image)
- {
- String imageString = null;
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- try
- {
- ImageIO.write(image, "png", bos);
- byte[] imageBytes = bos.toByteArray();
- BASE64Encoder encoder = new BASE64Encoder();
- imageString = encoder.encode(imageBytes);
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return imageString;
- }
- public static String imgurUpload(BufferedImage image) throws Exception
- {
- String clientID = "790910198fb7921";
- //create needed strings
- String address = "https://api.imgur.com/3/image.json";
- //Create HTTPClient and post
- HttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost(address);
- String dataImage = encodeToString(image);
- //add header
- post.addHeader("Authorization", "Client-ID " + clientID);
- //add image
- List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
- nameValuePairs.add(new BasicNameValuePair("image", dataImage));
- post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- //execute
- HttpResponse response = client.execute(post);
- //read response
- BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- //loop through response
- String line = rd.readLine();
- StringBuffer sb = new StringBuffer();
- while (line != null)
- {
- sb.append(line);
- line = rd.readLine();
- }
- String all = sb.toString();
- JSONObject jo = new JSONObject(all);
- JSONObject data = jo.getJSONObject("data");
- String link = null;
- if (data.has("link"))
- {
- link = data.getString("link");
- } else if (data.has("error")) {
- link = data.getString("error");
- }
- return link;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement