How can I make a multipart/form-data POST request using Java?

后端 未结 11 1671
攒了一身酷
攒了一身酷 2020-11-22 05:56

In the days of version 3.x of Apache Commons HttpClient, making a multipart/form-data POST request was possible (an example from 2004). Unfortunately this is no longer possi

11条回答
  •  攒了一身酷
    2020-11-22 06:22

    These are the Maven dependencies I have.

    Java Code:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    
    FileBody uploadFilePart = new FileBody(uploadFile);
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("upload-file", uploadFilePart);
    httpPost.setEntity(reqEntity);
    
    HttpResponse response = httpclient.execute(httpPost);
    

    Maven Dependencies in pom.xml:

    
      org.apache.httpcomponents
      httpclient
      4.0.1
      compile
    
    
      org.apache.httpcomponents
      httpmime
      4.0.1
      compile
    
    

提交回复
热议问题