I can't figure out what I am possible doing wrong when trying to upload an image in Java by doing exactly what is mentioned in the API.
If I set ?source=<some base64 byte array> I get an error and no upload. But if ?source=<some internet image url> it works just fine.
This is my code:
My Error:
Any help is appreciated! Thanks
If I set ?source=<some base64 byte array> I get an error and no upload. But if ?source=<some internet image url> it works just fine.
This is my code:
Code:
// Creates Byte Array from picture
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write((RenderedImage)image, "jpg", baos);
URL url = new URL(URI); // http://stppl.us/api/1/upload/
byte[] bytes = baos.toByteArray();
// encodes picture with Base64 and inserts api key
String base64bytes = Base64.encode(bytes);
String data = "?key=" + KEY;
data += "&source=" + base64bytes;
data += "&format=json";
// opens connection and sends data
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(data);
out.flush();
out.close();
DataInputStream in = new DataInputStream(conn.getInputStream());
String decodedString;
while (null != ((decodedString = in.readLine())))
{
//temporary. Should print out a json response if working correctly
System.out.println(decodedString);
}
in.close();
//wr.close();
My Error:
Code:
java.io.IOException: Server returned HTTP response code: 400 for URL: http://stppl.us/api/1/upload
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.shaneisrael.st.stppus.StppUploader.run(StppUploader.java:112)
at java.lang.Thread.run(Unknown Source)
Any help is appreciated! Thanks