java - JSP: Get MIME Type on File Upload -
i'm doing file upload, , want mime type uploaded file.
i trying use request.getcontenttype(), when call:
string contenttype = req.getcontenttype();
it return:
multipart/form-data; boundary=---------------------------310662768914663
how can correct value?
thanks in advance
it sounds if you're homegrowing multipart/form-data
parser. wouldn't recommend that. rather use decent 1 apache commons fileupload. uploaded files, offers fileitem#getcontenttype()
extract client-specified content type, if any.
string contenttype = item.getcontenttype();
if returns null
(just because client didn't specify it), can take benefit of servletcontext#getmimetype()
based on file name.
string filename = filenameutils.getname(item.getname()); string contenttype = getservletcontext().getmimetype(filename);
this resolved based on <mime-mapping>
entries in servletcontainer's default web.xml
(in case of example tomcat, it's present in /conf/web.xml
) , on web.xml
of webapp, if any, can expand/override servletcontainer's default mappings.
you need keep in mind value of multipart content type controlled client , client-provided file extension not need represent actual file content. instance, client edit file extension. careful when using information in business logic.
Comments
Post a Comment