Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.boamoradia.code;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import javax.imageio.stream.FileImageInputStream;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.InputStreamEntity;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.impl.client.BasicResponseHandler;
- import org.apache.http.impl.client.DefaultHttpClient;
- public class Main {
- public static void main(String[] args) {
- Main m = new Main();
- try {
- m.test();
- } catch (ClientProtocolException e) {
- System.out.println("PROTOCOL ERROR:");
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("IO ERROR:");
- e.printStackTrace();
- }
- }
- public void test() throws ClientProtocolException, IOException{
- String url = "http://localhost";
- File f = new File("/home/armando/Downloads/inteligências.txt");
- HttpClient client = new DefaultHttpClient();
- //HttpGet request = new HttpGet(url);
- HttpPost request = new HttpPost(url);
- MultipartEntityBuilder b = MultipartEntityBuilder.create();
- b.addBinaryBody("file",f,ContentType.APPLICATION_OCTET_STREAM,"file.ext");
- //b.addTextBody("name","armando");
- HttpEntity ett = b.build();
- request.setEntity(ett);
- HttpResponse response = client.execute(request);
- String responseString = new BasicResponseHandler().handleResponse(response);
- System.out.println(responseString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement