Skip to content

Instantly share code, notes, and snippets.

@brenodouglas
Created December 2, 2013 02:30
Show Gist options
  • Select an option

  • Save brenodouglas/7744044 to your computer and use it in GitHub Desktop.

Select an option

Save brenodouglas/7744044 to your computer and use it in GitHub Desktop.
public String uploadServer() {
try {
File file = new File("String url da foto");
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("url para enviar o post");
ContentType contentType = ContentType.create("image/png");
String boundary = String.valueOf(System.currentTimeMillis());
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.setBoundary(boundary);
entity.addBinaryBody("img", file, contentType, "String url da foto" );
postRequest.setEntity(entity.build());
HttpResponse response = httpClient.execute(postRequest);
return getContent(response);
} catch(Exception e){
return e.getMessage();
}
}
public static String getContent(HttpResponse response) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
String content = "";
while ((body = rd.readLine()) != null)
{
content += body + "\n";
}
return content.trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment