1

In my java web application I have a rest service to receive files via post as below:

@Path("/upload")
@Component
public class UploadResource {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @POST
    @Path("/files")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void uploadFiles(@RequestParam("files") MultipartFile[] multipartFiles, @RequestParam("email") String email) {

        for (MultipartFile multipartFile : multipartFiles) {
            //Do something
        }
    }
}

I would like an example of how to implement a client that is able to send a list of files all in one time for that service using apache HttpClient. Can someone help me?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
araraujo
  • 613
  • 2
  • 8
  • 17

1 Answers1

0
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpEntity entity = MultipartEntityBuilder
            .create()
            .addBinaryBody("files", new File("C:\\temp\\index.html"), ContentType.create("application/octet-stream"), "filename1")
            .addBinaryBody("files", new File("C:\\temp\\index.html"), ContentType.create("application/octet-stream"), "filename2")
            .addTextBody("email", "agree")
            .build();

    HttpPost httpPost = new HttpPost("http://localhost:8080/files");
    httpPost.setEntity(entity);
    HttpResponse response = httpClient.execute(httpPost);
user2880879
  • 297
  • 1
  • 3