0

I am trying to make a HTTP POST call to upload a file (and also has to fill parameters ex. title) to restful web service:

This is what I tried to do:

    File file = new File(filepath);

    HttpClient client = new DefaultHttpClient(); //DefaultHTTPClient is deprecated?
    HttpPost post = new HttpPost(url);

    //Multipart Entity is also deprecated?? 
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("file[path]", new FileBody(file));
    post.setEntity((HttpEntity) entity);

    try {
        HttpResponse response = client.execute(post);

    } catch (IOException e) {

        e.printStackTrace();
    }

This is not working. I am not sure how to upload the file and add the parameters and make the call? I tried this using Java Http Client to upload file over POST but the ones he/she used is deprecated? I have httpcore 4.4 alpha jar

Community
  • 1
  • 1
Raj kobie
  • 93
  • 2
  • 10

1 Answers1

0

Yes the classes you use are deprecated. Try this example:

https://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

Filip Nguyen
  • 1,029
  • 1
  • 7
  • 20