I want to build support for fileupload using Jersey (2.22.2) & JaxRs.
This is my application:
@ApplicationPath("rest")
public class DsmJaxRsApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(FileUploadResource.class);
classes.add(MultiPartFeature.class);
return classes;
}
}
This is my resource method to intercept the POST request:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Path("upload")
public Response uploadImportFile(@FormDataParam("reason") String reason,
@FormDataParam("fileName") String fileName,
@FormDataParam("file") InputStream fileContent) {
checkCreateBulkChangeAllowed();
return Response.ok().build();
}
And this is my request payload:
------WebKitFormBoundaryAh9J98cKgsOv6WCr
Content-Disposition: form-data; name="reason"
wwwww
------WebKitFormBoundaryAh9J98cKgsOv6WCr
Content-Disposition: form-data; name="fileName"
DSM_CH_Bulk_new.xlsx
------WebKitFormBoundaryAh9J98cKgsOv6WCr
Content-Disposition: form-data; name="file"; filename="DSM_CH_Bulk_new.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
------WebKitFormBoundaryAh9J98cKgsOv6WCr--
The problem is that inside the method both the fileContent
and the reason
contain "wwww" which should be the reason and the fileName
is always null
. Does anyone have any idea what am I doing wrong or what am I missing here?
My configuration was based in a web.xml file. Try to add FormDataContentDisposition
UPDATE
Here is my servlet in my web.xml file:
and here are my dependencies: