Search code examples
javavalidationmime

Validation to check whether its a ".txt" file


I have this particular piece of code for restricting the users to upload image files only.

  if (!fileName.getContentType().startsWith("image/")) 
  errors.add("", new ActionError("errors.imageFile.contentType"));

Similary I want the users to upload only files with extension ".txt" in another scenario. What MIME type should I use or please let me know the code which will be helpful for achieving this task.


Solution

  • Typically the mime type for text files is text/plain