Hello folks, looking for a bit of help with something I'm working on but can't seem to find a solution to. I'm trying to upload an image to tumblr from java - and unfortunately no matter what I try it returns a 403 forbidden error - although unfortunately the cause of this is not as simple as a typo in my password or username

I've been looking at how other people have tried it, but I can't find a successful example of how to do it. Btw Post requests to send text content to tumblr work perfectly, so I'm pretty certain the problem is related to the image encoding.
Does anybody have any ideas, or perhaps can spot any glaring errors in my code? I've been playing around with it so much I don't know what to make of it anymore myself! Thanks in advance.
//edit: here's the tumblr api page:
http://www.tumblr.com/docs/en/api#api_write
Here's the summarised version of my code:
import javax.imageio.*;
import java.awt.image.*;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
public void postImage(String srcImage) {
BufferedImage imageToWrite = null;
String username = "email";
String password = "password";
String enc = "UTF-8"; //encoding type
String type = "photo"; //type of tumblr post
String title = "test"; //the title of the post
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//write the file into the ByteArrayOutputStream stream for uploading
File file = new File(srcImage);
imageToWrite = ImageIO.read(file);
ImageIO.write(imageToWrite, "jpg", stream);
//the Tumblr api requests at least an email, password, post-type, image-source, and title to make the post
//encoding the url
String data = URLEncoder.encode("email", enc) + "=" + URLEncoder.encode(username, enc);
data += "&" + URLEncoder.encode("password", enc) + "=" + URLEncoder.encode(password, enc);
data += "&" + URLEncoder.encode("type", enc) + "=" + URLEncoder.encode(type, enc);
data += "&" + URLEncoder.encode("data", enc) + "=" + URLEncoder.encode(stream.toString(), enc);
data += "&" + URLEncoder.encode("title", enc) + "=" + URLEncoder.encode(title, enc);
//make a connection, set http parameters
URL tumblr = new URL("http://www.tumblr.com/api/write");
HttpURLConnection http = (HttpURLConnection) tumblr.openConnection();
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "multipart/form-data"); //tumblr needs multipart/form-data
OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream());
//connect and send the url string data
http.connect();
out.write(data);
out.flush();
System.out.println(http.getResponseCode());
System.out.println(http.getResponseMessage());
out.close();
}
catch(IOException e) {
e.printStackTrace();
}
}