Access parts of multipart / form-data submission request in Java REST web service

I have a multi-page form that should load a file, as well as some parameters. It looks like this:

<form id="upload" action="http://localhost:9998/test" method="post" enctype="multipart/form-data"> <input name="inputfile" type="file" size="50" accept="application/octet-stream"> <input name="someparameter" type="text" size="10"> <input type="submit" value="Go!"> </form> 

The web service is as follows:

 @Path("/test") public class ServiceInterface { @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void execute(@FormParam(value="someparameter") String param) { System.out.println(param); } } 

When submitting a form, the value for "someparameter" is always indicated as null, but on the form I entered a value.

My questions:

  • What is wrong with the code above?
  • How do I access a file that is submitted using a form?

I am using Jersey 1.10.

+6
source share
1 answer

Well, after a few minutes of working on Google, I found an error in my code.

You should use the @FormDataParam annotation instead of @FormParam.

The resulting code is as follows:

 @Path("/test") public class ServiceInterface { @POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void execute( @FormDataParam("someparameter") String param @FormDataParam("inputfile") File inputfile ) { System.out.println(param); } } 
+14
source

Source: https://habr.com/ru/post/911959/


All Articles