Question

I have a Java web application to allow users to upload Word or PDF file. I have 2 users complaining Wrong file type when uploading a .DOCX file. I got both files and uploaded in my own PC. Both worked smoothly.

I use MultipartRequest.getContentType() to get the file type. Then use the following to check

if(!(myFileType.equals("application/pdf") || myFileType.equals("application/msword") || myFileType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))){
                errMsg = "Wrong file type. Only PDF and MS Word are accepted";          
}

It seems to me that it is an issue with client PC. Browser? AntiVirus software? Does anyone have clue?

[added] I found in the log file that some PDF files have wrong type too. I do need to log mime type if there is wrong file type error.

[added 2] I asked one user to upload it again after the program was modified. Guess what? No error this time. Faint!

Was it helpful?

Solution

We found in our application that some browsers reported .docx as zip (mimetype application/zip or application/x-zip-compressed). This is probably because .docx files are in fact zipped files and the browsers were doing some magic number scanning or somesuch.

Basically, just don't trust mimetypes as reported by client software. Don't trust extensions either.

OTHER TIPS

Short answer is no, different browsers do NOT have different mime types. However, docx won't work with application/msword. See this link for the correct mime types for office 2007+ docx etc files:

http://www.kattare.com/docs/faq_view/822/how-do-i-add-microsoft-office-2007-support-mime-types-to-tomcat.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top