Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MIME type of uploaded file in Jersey

I have a standard upload endpoint in Jersey:

@POST
@Secure
@Consumes("multipart/form-data")
public Response upload( @Context final HttpHeaders hh,
            @FormDataParam("fileaaa") final FormDataContentDisposition disposition,
            @FormDataParam("fileaaa") final InputStream input,

How can I get the MIME type of the uploaded file?

If I do disposition.getType this gets me the MIME type of the form; in this case form-data.

I know the information is there somewhere; the HTTP message should be something like:

-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="input_text"

mytext

-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="fileaaa";
filename="C:\Inetpub\wwwroot\Upload\pic.gif"
Content-Type: image/gif

(binary content)
-----------------------------7d01ecf406a6--
like image 893
Cristian Vrabie Avatar asked Nov 11 '10 17:11

Cristian Vrabie


People also ask

What is MIME type in file upload?

A media type (also known as a Multipurpose Internet Mail Extensions or MIME type) indicates the nature and format of a document, file, or assortment of bytes. MIME types are defined and standardized in IETF's RFC 6838.


1 Answers

I solved this by letting Jersey inject a BodyPart parameter in my method. getMediaType() on the body part gives me what I needed:

@POST
@Secure
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(/*other parms */,  @FormDataParam("fileaaa") final FormDataBodyPart body) {
   String mimeType = body.getMediaType().toString();
   //handle upload
}
like image 173
Cristian Vrabie Avatar answered Oct 25 '22 06:10

Cristian Vrabie