Skip to content

Instantly share code, notes, and snippets.

@mrbaboon
Last active September 7, 2015 16:03
Show Gist options
  • Save mrbaboon/85a52b4f7d549d844005 to your computer and use it in GitHub Desktop.
Save mrbaboon/85a52b4f7d549d844005 to your computer and use it in GitHub Desktop.
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
if (args.length != 1) {
System.out.println("File path not given");
System.exit(1);
}
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost("http://dev.smartpager.net:8000/rest/putFile");
StringBody mimeType = new StringBody("image/jpeg", ContentType.TEXT_PLAIN);
File imageFile = new File(args[0]);
FileBody bin = new FileBody(imageFile);
HttpEntity regEntity = MultipartEntityBuilder.create()
.addPart("binaryFile", bin)
.addPart("mimeType", mimeType)
.build();
httppost.setEntity(regEntity);
System.out.println("Executing request: " + httppost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
System.out.println("Hello World!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment