Created
June 26, 2014 21:01
-
-
Save DV8FromTheWorld/b29f0825550060b19c98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.dv8tion; | |
import java.awt.image.BufferedImage; | |
import java.io.BufferedReader; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.List; | |
import javax.imageio.ImageIO; | |
import javax.xml.bind.DatatypeConverter; | |
public class Uploader | |
{ | |
//CHANGE TO @CLIENT_ID@ and replace with buildscript. | |
private final static String CLIENT_ID = "efce6070269a7f1"; | |
//CHANGE TO @CLIENT_SECRET@ and replace with buildscript. | |
private final static String CLIENT_SECRET = "bc038b940212ad28e0de3eddea1a18375434b143"; | |
public static String upload(BufferedImage image) | |
{ | |
// MemoryStream ms = new MemoryStream(); | |
// ImageFormat format = image.RawFormat; | |
// image.Save(ms, format.Equals(ImageFormat.MemoryBmp) ? ImageFormat.Png : format); | |
// WebClientEx w = new WebClientEx(); | |
// w.timeout = 100000; | |
// w.Headers.Add("Authorization", "Client-ID " + CLIENT_ID); | |
HttpURLConnection conn = getHttpConnection("https://api.imgur.com/3/image"); | |
// NameValueCollection values = new NameValueCollection | |
// { | |
// { "image", toBase64(image) } | |
// }; | |
// byte[] response = w.UploadValues("https://api.imgur.com/3/image", values); | |
writeToConnection(conn, "image=" + toBase64(image)); | |
return getResponse(conn); | |
} | |
public static String createAlbum(List<String> imageIds) | |
{ | |
HttpURLConnection conn = getHttpConnection("https://api.imgur.com/3/album"); | |
String ids = ""; | |
for (String id : imageIds) | |
{ | |
if (!ids.equals("")) | |
{ | |
ids += ","; | |
} | |
ids += id; | |
} | |
// NameValueCollection values = new NameValueCollection | |
// { | |
// { "ids", ids } | |
// }; | |
// byte[] response = w.UploadValues("https://api.imgur.com/3/album", values); | |
writeToConnection(conn, "ids=" + ids); | |
return getResponse(conn); | |
} | |
private static String toBase64(BufferedImage image) | |
{ | |
ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); | |
try | |
{ | |
ImageIO.write(image, "png", byteArray); | |
return URLEncoder.encode(DatatypeConverter.printBase64Binary(byteArray.toByteArray()), "UTF-8"); | |
} | |
catch (UnsupportedEncodingException e) | |
{ | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static HttpURLConnection getHttpConnection(String url) | |
{ | |
HttpURLConnection conn; | |
try | |
{ | |
conn = (HttpURLConnection) new URL(url).openConnection(); | |
conn.setDoInput(true); | |
conn.setDoOutput(true); | |
conn.setRequestMethod("POST"); | |
conn.setRequestProperty("Authorization", "Client-ID " + CLIENT_ID); | |
conn.setReadTimeout(100000); | |
conn.connect(); | |
return conn; | |
} | |
catch (MalformedURLException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static void writeToConnection(HttpURLConnection conn, String message) | |
{ | |
OutputStreamWriter writer; | |
try | |
{ | |
writer = new OutputStreamWriter(conn.getOutputStream()); | |
writer.write(message); | |
writer.flush(); | |
writer.close(); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private static String getResponse(HttpURLConnection conn) | |
{ | |
StringBuilder str = new StringBuilder(); | |
BufferedReader reader; | |
try | |
{ | |
reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String line; | |
while ((line = reader.readLine()) != null) | |
{ | |
str.append(line); | |
} | |
reader.close(); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return str.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment