Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glassfish jersey FormDataContentDisposition getSize returning -1

I Have a Jersey RESTful webservice under Glassfish which accepts incoming POST requests for uploading images, consuming MediaType.MULTIPART_FORM_DATA which maps to mime type multipart.

When I receive the instance of FormDataContentDisposition fileDetail in my service and call fileDetail.getSize()

I always get -1

I wonder how is the appropriate way to fetch the correct file size using Jersey and Multipart file upload.

like image 905
guilhebl Avatar asked Nov 04 '15 06:11

guilhebl


1 Answers

I also encountered this problem and I'm now seeking for RCA and solutions.

Just a workaround: save the file temporarily and use File#length() method to get the real size of uploaded file.

More update:

As complement of the above method, keep monitoring saved bytes' size, throw an exception (or do what the policy requires) when the file size threshold is reached.

What's more, don't trust the size given directly by client, which could be faked, unless the client is trusted.

like image 190
Silent Avatar answered Oct 22 '22 15:10

Silent