Skip to content

Instantly share code, notes, and snippets.

@aanoaa
Created September 4, 2010 07:48
Show Gist options
  • Save aanoaa/565000 to your computer and use it in GitHub Desktop.
Save aanoaa/565000 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class SubmitTest {
// Constants
private static final String NEWLINE = "\r\n";
private static final String PREFIX = "--";
private DataOutputStream out;
private String boundary;
public static void main(String args[]) {
try {
URL url = new URL("http://example.com"); // URL 을 여따가 적으세여
URLConnection urlConn = SubmitTest.createConnection(url);
urlConn.setRequestProperty("Accept", "*/*");
urlConn.setRequestProperty("Accept-Encoding", "gzip");
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("Cache-Control", "no-cache");
String boundary = SubmitTest.createBoundary();
urlConn.setRequestProperty("Content-Type", SubmitTest.getContentType(boundary));
SubmitTest out = new SubmitTest(urlConn.getOutputStream(), boundary);
out.writeField("foo", "foo"); // text field
out.writeFile("photo", "text/jpeg", new File("res/life.jpeg")); // file field
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String line = "";
while((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
public SubmitTest(OutputStream os, String boundary) {
if (os == null) {
throw new IllegalArgumentException("outputstream required");
}
if (boundary == null || boundary.length() == 0) {
throw new IllegalArgumentException("boundary required");
}
this.out = new DataOutputStream(os);
this.boundary = boundary;
}
public void writeField(String name, String value) throws java.io.IOException {
if(name == null) {
throw new IllegalArgumentException("Name cannot be null or empty.");
}
if(value == null) {
value = "";
}
try {
InputStream is = new ByteArrayInputStream(value.getBytes("UTF-8"));
byte[] data = new byte[1024];
int r = 0;
out.writeBytes(PREFIX);
out.writeBytes(boundary);
out.writeBytes(NEWLINE);
out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"");
out.writeBytes(NEWLINE);
out.writeBytes(NEWLINE);
while((r = is.read(data, 0, data.length)) != -1) {
out.write(data, 0, r);
}
out.writeBytes(NEWLINE);
out.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void writeFile(String name, String mimeType, File file) throws java.io.IOException {
if(file == null) {
throw new IllegalArgumentException("File cannot be null.");
}
if(!file.exists()) {
throw new IllegalArgumentException("File does not exist.");
}
if(file.isDirectory()) {
throw new IllegalArgumentException("File cannot be a directory.");
}
writeFile(name, mimeType, file.getCanonicalPath(), new FileInputStream(file));
}
public void writeFile(String name, String mimeType, String fileName, InputStream is) throws java.io.IOException {
if(is == null) {
throw new IllegalArgumentException("Input stream cannot be null.");
}
if(fileName == null || fileName.length() == 0) {
throw new IllegalArgumentException("File name cannot be null or empty.");
}
// write boundary
out.writeBytes(PREFIX);
out.writeBytes(boundary);
out.writeBytes(NEWLINE);
// write content header
out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + fileName + "\"");
out.writeBytes(NEWLINE);
if(mimeType != null) {
out.writeBytes("Content-Type: " + mimeType);
out.writeBytes(NEWLINE);
}
out.writeBytes(NEWLINE);
// write content
byte[] data = new byte[1024];
int r = 0;
while((r = is.read(data, 0, data.length)) != -1) {
out.write(data, 0, r);
}
try {
is.close();
} catch(Exception e) {}
out.writeBytes(NEWLINE);
out.flush();
}
public void close() throws java.io.IOException {
// write final boundary
out.writeBytes(PREFIX);
out.writeBytes(boundary);
out.writeBytes(PREFIX);
out.writeBytes(NEWLINE);
out.flush();
out.close();
}
public String getBoundary() {
return this.boundary;
}
public static URLConnection createConnection(URL url) throws java.io.IOException {
URLConnection urlConn = url.openConnection();
if(urlConn instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection)urlConn;
httpConn.setRequestMethod("POST");
}
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setDefaultUseCaches(false);
return urlConn;
}
public static String createBoundary() {
return "--------------------" +
Long.toString(System.currentTimeMillis(), 16);
}
public static String getContentType(String boundary) {
return "multipart/form-data; boundary=" + boundary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment