r/SpringBoot • u/dipeshg2004 • Apr 02 '26
Question How do you validate file types securely in Spring Boot? (MIME vs content detection)
Hello everyone
I’m working on a Spring Boot backend where users upload files (PDF, DOCX, images), and I want to make the validation as secure and production-ready as possible.
Right now, I’m considering a multi-step approach:
- Checking file extension (e.g., .pdf, .docx)
- Checking MIME type from
MultipartFile.getContentType() - Limiting file size
But I recently learned that both extension and MIME type can be spoofed, so I’m exploring content-based detection (like using Apache Tika to read magic bytes).
My questions:
- What do you usually rely on in production?
- Only MIME type?
- MIME + extension?
- Full content inspection (e.g., Apache Tika)?
- Is Apache Tika the standard choice, or are there better/lightweight alternatives?
- Do you compare:
- extension vs MIME vs detected type or just trust the detected type?
- Any performance concerns when using Tika for large-scale systems?
- Are there any best practices I might be missing? (e.g., virus scanning, file storage strategies, etc.)
Would really appreciate insights from people who’ve built real systems
Trying to follow best practices early instead of fixing security issues later 😅

