Created
October 8, 2010 20:50
-
-
Save kageroh/617519 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
import java.io.BufferedReader; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Random; | |
import java.util.Set; | |
public class MultipartForm { | |
private int mContentLength; | |
private String mBoundary; | |
private String mCharset; | |
private String mPath; | |
private HashMap<String, String> mHash; | |
public MultipartForm(String charset) { | |
mCharset = charset; | |
mBoundary = generateBoundary(); | |
} | |
public void ready(HashMap<String, String> hash, String path) throws IOException { | |
mHash = hash; | |
mPath = path; | |
doOutputDummy(); | |
} | |
public String post(String serv) throws IOException { | |
URL url = new URL(serv); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setFixedLengthStreamingMode(mContentLength); | |
conn.setRequestMethod("POST"); | |
conn.setDoOutput(true); | |
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + mBoundary); | |
conn.connect(); | |
OutputStream out = conn.getOutputStream(); | |
doOutput(out); | |
out.flush(); | |
out.close(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String responseData = null; | |
StringBuffer buf = new StringBuffer(); | |
while( (responseData = reader.readLine()) != null ) { | |
buf.append(responseData); | |
} | |
reader.close(); | |
conn.disconnect(); | |
return buf.toString(); | |
} | |
private String generateBoundary() { | |
StringBuffer buf = new StringBuffer(); | |
for (int i = 0; i < 29; i++) { | |
buf.append("-"); | |
} | |
String chars = "1234567890"; | |
Random rand = new Random(); | |
for (int i = 0; i < 29; i++) { | |
int r = rand.nextInt(chars.length()); | |
buf.append(chars.substring(r, r + 1)); | |
} | |
return buf.toString(); | |
} | |
private void doOutputDummy() throws IOException { | |
DummyOutputStream dummy = new DummyOutputStream(); | |
doOutput(dummy); | |
mContentLength = dummy.getSize(); | |
dummy.close(); | |
} | |
private void doOutput(OutputStream out) throws IOException { | |
Set<String> set = mHash.keySet(); | |
Iterator<String> iterator = set.iterator(); | |
while (iterator.hasNext()) { | |
String key = iterator.next(); | |
out.write(("--" + mBoundary + "\r\n").getBytes(mCharset)); | |
out.write(("Content-Disposition: form-data; name=\"" + key + "\"\r\n").getBytes(mCharset)); | |
out.write(("Content-Type: text/plain; charset=" + mCharset + "\r\n\r\n").getBytes(mCharset)); | |
out.write((mHash.get(key)).getBytes(mCharset)); | |
out.write(("\r\n").getBytes(mCharset)); | |
} | |
if (mPath != null) { | |
out.write(("--" + mBoundary + "\r\n").getBytes(mCharset)); | |
out.write(("Content-Disposition: form-data; name=\"file\"; filename=\"out.jpg\"\r\n").getBytes(mCharset)); | |
out.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes(mCharset)); | |
InputStream in = null; | |
byte[] bytes = new byte[1024]; | |
in = new FileInputStream(mPath); | |
if (in != null) { | |
while (true) { | |
int ret = in.read(bytes); | |
if (ret == -1) { | |
break; | |
} | |
out.write(bytes, 0, ret); | |
out.flush(); | |
} | |
in.close(); | |
} | |
out.write(("\r\n").getBytes(mCharset)); | |
} | |
out.write(("--" + mBoundary + "--").getBytes(mCharset)); | |
} | |
} | |
class DummyOutputStream extends OutputStream { | |
private int size = 0; | |
@Override | |
public void write(int b) throws IOException { | |
size += 1; | |
} | |
@Override | |
public void write(byte[] bytes) throws IOException { | |
size += bytes.length; | |
} | |
@Override | |
public void write(byte[] b, int off, int len) throws IOException { | |
size += len; | |
} | |
public int getSize(){ | |
return this.size; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment