Skip to content

Instantly share code, notes, and snippets.

@cathandnya
Created May 10, 2013 02:24
Show Gist options
  • Save cathandnya/5552015 to your computer and use it in GitHub Desktop.
Save cathandnya/5552015 to your computer and use it in GitHub Desktop.
multipart posting for Tumblr on Android.
private static void addValue(String val, String key, OutputStream strm, String boundary) throws UnsupportedEncodingException, IOException {
strm.write(boundary.getBytes("UTF-8"));
strm.write("\r\n".getBytes("UTF-8"));
strm.write("Content-Disposition: form-data; name=\"".getBytes("UTF-8"));
strm.write(key.getBytes("UTF-8"));
strm.write("\"\r\n\r\n".getBytes("UTF-8"));
strm.write(val.getBytes("UTF-8"));
strm.write("\r\n".getBytes("UTF-8"));
}
private static void addValue(File val, String key, OutputStream strm, String boundary) throws IOException {
InputStream is = new FileInputStream(val);
byte[] indata = new byte[(int)val.length()];
is.read(indata);
is.close();
strm.write(boundary.getBytes("UTF-8"));
strm.write("\r\n".getBytes("UTF-8"));
strm.write("Content-Disposition: file; name=\"".getBytes("UTF-8"));
strm.write(key.getBytes("UTF-8"));
strm.write("\"; filename=\"file.jpg\"\r\n".getBytes("UTF-8"));
strm.write("Content-Type: image/jpeg\r\n\r\n".getBytes("UTF-8"));
strm.write(indata);
strm.write("\r\n".getBytes("UTF-8"));
}
public static HttpURLConnection multipartPost(String _url, List<NameValuePair> parameters, List<String> fileKeys, List<File> files, OAuthConsumer consumer) throws UnsupportedEncodingException, IOException, OAuthMessageSignerException, OAuthExpectationFailedException, OAuthCommunicationException {
final String boundary = "------------0xKhTmLbOuNdArY";
HttpURLConnection con = null;
con = (HttpURLConnection) new URL(_url).openConnection();
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setConnectTimeout(60 * 1000);
HttpParameters hp = new HttpParameters();
Iterator<NameValuePair> pit = parameters.iterator();
while (pit.hasNext()) {
NameValuePair pair = (NameValuePair)pit.next();
String parameterKey = pair.getName();
String parameterValue = pair.getValue();
parameterValue = URLEncoder.encode(parameterValue, "UTF-8");
parameterValue = parameterValue.replace("+", "%20");
hp.put(parameterKey, parameterValue);
}
if (consumer != null) {
consumer.setAdditionalParameters(hp);
consumer.sign(con);
}
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary.substring(2));
OutputStream strm = con.getOutputStream();
Iterator<String> itr = hp.keySet().iterator();
while (itr.hasNext()) {
String parameterKey = itr.next();
String parameterValue = hp.getFirst(parameterKey, true);
addValue(parameterValue, parameterKey, strm, boundary);
}
Iterator<String> ki = fileKeys.iterator();
Iterator<File> fi = files.iterator();
while (ki.hasNext() && fi.hasNext()) {
addValue(fi.next(), ki.next(), strm, boundary);
}
strm.write(boundary.getBytes("UTF-8"));
strm.write("--\r\n".getBytes("UTF-8"));
strm.flush();
con.connect();
return con;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment