Created
December 2, 2013 02:30
-
-
Save brenodouglas/7744044 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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