I'm trying to send the multipartentity using httpclient(Post) to spring module which is running in server. So it is restful web service. But my code is throwing error. Can you please help me out?
I already tried this: Spring : File Upload RESTFUL Web Service
HTTPCLIENT FILE:(POST REQUEST)
public class MultiFile {
public static void main(String args[]) throws ClientProtocolException, IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8080/RESTapi/student/addimage");
File file = new File("/Users/prabhu-pt3030/Desktop/eclipse-workspace-new/testing/target/classes/tes/javaFile123.txt");
MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
FileBody filebody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
entitybuilder.addPart("files", filebody);
HttpEntity mutiPartHttpEntity = entitybuilder.build();
httppost.setEntity(mutiPartHttpEntity);
HttpResponse httpresponse = httpclient.execute(httppost);
}
}
Spring controller:
@RequestMapping(value="/student/addimage",method=RequestMethod.POST,headers = "content-type=multipart/form-data")
public void Addingimage(@RequestParam(value="files") MultipartFile files)
{
//System.out.println(files.isEmpty());
}
Output:
Required MultipartFile parameter 'files' is not present