It sounds like you are creating a multipart/form-data parser. I would not recommend doing this. Rather, use a decent one, such as Apache Commons FileUpload . For downloaded files, it offers FileItem#getContentType() to retrieve the specified content type, if any.
String contentType = item.getContentType();
If it returns null (just because the client did not specify it), you can use ServletContext#getMimeType() based on the file name.
String filename = FilenameUtils.getName(item.getName()); String contentType = getServletContext().getMimeType(filename);
This will be allowed based on the <mime-mapping> entries in the servletcontainer default web.xml (in the case of, for example, Tomcat, it is present in /conf/web.xml ), as well as on the web.xml your web client, if any there is one that can expand / override the default collation for servletcontainer.
However, you need to remember that the value of the type of multi-page content is completely controlled by the client, and also that the file extension provided by the client does not have to represent the actual contents of the file. For example, a client may simply edit the file extension. Be careful when using this information in business logic.
Connected:
- How to upload files to JSP / Servlet?
- How to check if the downloaded file is an image?
Balusc
source share