How to specify external location for image store in Spring Boot 2.0 web app?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In a Spring Boot web application, storing uploaded images outside the packaged application directory is usually the right design. It avoids losing files on redeploy, keeps the application artifact immutable, and makes storage paths configurable per environment. The clean approach is to externalize the storage directory into configuration, write files through a service, and expose them through a mapped resource handler or controller.
Put the Storage Path in Configuration
Do not hardcode filesystem paths inside controllers. Use a property and bind it once.
Then map it into a typed configuration class:
That keeps the actual location environment-specific and easy to override.
Save Files Through a Dedicated Service
The service should normalize paths, create directories if needed, and reject dangerous filenames.
The startsWith(root) check prevents path traversal through malicious upload names.
Expose the External Directory as Static Content
If you want images to be served by Spring MVC, register a resource handler that points to the external filesystem directory.
With that in place, a file stored as example.png under the configured directory becomes available at /images/example.png.
Keep Deployment and Permissions in Mind
External storage only works if the application process can read and write the directory. In real deployments, check:
- directory ownership
- filesystem permissions
- mounted volume availability in containers
If the path is invalid or not writable, fail fast at startup or during the first save operation with a clear error rather than a generic 500 response.
When Filesystem Storage Is Enough
External filesystem storage is a good fit for a single server or a simple internal application. Once you need multiple instances, shared storage or object storage such as S3 usually becomes a better design. The application code still benefits from the same abstraction, because only the backing storage service changes.
That is another reason to avoid putting filesystem logic directly inside controllers. A storage service gives you a migration path when local disk stops being the right backend.
Common Pitfalls
- Hardcoding upload directories in controllers instead of configuration.
- Writing files under
src/main/resourcesand losing them on redeploy. - Trusting the original filename without normalization and path checks.
- Forgetting to configure a resource handler or controller to serve the files back.
- Deploying to containers without mounting a persistent writable directory.
Summary
- Store image paths in configuration, not in code.
- Write files through a dedicated service that validates and normalizes paths.
- Use a resource handler to expose the external directory over HTTP when needed.
- Check directory permissions and persistence strategy in deployment.
- Filesystem storage is fine for simple deployments, but abstraction helps future migration to shared or cloud storage.
Related reading
- How to specify function types for void not Void methods in Java8?
- How to specify jackson to only use fields - preferably globally
- How to specify Java version in Spring/Spring Boot pom.xml?
- How to specify packagesToScan in HibernateJpaAutoConfiguration?
- How to specify prefix for all controllers in Spring Boot?
- How to specify the JDK version in Android Studio?
- How to split a delimited string to a ListString
- How to split a String by space
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.