1

How do you create an endpoint for someone to upload a image using jax-rs without jersey?

this is the code I have written:

@POST
@Path("/uploadImage/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@QueryParam("uan") String uan, InputStream stream) {
    try {
        byte[] attachmentBytes = IOUtils.toByteArray(stream);
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(attachmentBytes));
        return Response.ok().build();
    } catch (IOException e) {
        System.out.print(e);
        return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
    }

}

When I hit the endpoint with or without file I get a 200 OK response.

There are many examples of creating an file upload endpoint using Jersey however i do not want to have to import Jersey just for this one task. This post has an answer using Jersey that does what I want to do: FileUpload with JAX-RS

D.Rees
  • 183
  • 2
  • 13
  • 1
    I would like the resource to accept .PDF, .JPEG and .PNG files also. Does this affect the way I handle the call? – D.Rees Jan 29 '18 at 13:32
  • 1
    Which JAX-RS implementation do you use instead of Jersey? –  Jan 29 '18 at 13:52
  • @LutzHorn using RESTeasy as JAX-RS implementation – D.Rees Jan 29 '18 at 13:59
  • Possible duplicate of [How do I do a multipart/form file upload with jax-rs?](https://stackoverflow.com/questions/2637017/how-do-i-do-a-multipart-form-file-upload-with-jax-rs) –  Jan 29 '18 at 14:00
  • Similar, although I can not resolve @PartType ? and I need to accept multiple MIME types: image/jpeg, image/png, and application/pdf. Their FileUploadForm class specifies a single MIME type so not sure how to get round this – D.Rees Jan 29 '18 at 14:39

0 Answers0