Stage 15 - File Upload and Download APIs
Backend code becomes hard to maintain when important behavior is hidden in random controller methods. This article focuses on multipart upload and streamed download. The topic looks technical, but the real question is practical: what happens when a real client sends a real request and something is incomplete, too large, forbidden, slow, or inconsistent?
Files are not ordinary JSON. They have size, media type, binary content, storage location, permissions, and download headers. A careless upload endpoint can fill disk or accept dangerous content.

The picture to keep in your head
Think about a small online shop. A user opens the frontend, clicks a button, and the browser calls the backend. The backend receives HTTP data, converts it into Java objects, checks rules, touches the database, and returns JSON. If each step has a clear owner, the system is understandable. If every step is mixed in one method, the first production incident becomes painful.
For this topic the sequence is:
- multipart request.
- size/type check.
- storage service.
- metadata row.
| Part | Responsibility |
|---|---|
| Controller | Receives HTTP data and returns the public response. |
| Service | Runs the business use case and protects business rules. |
| Repository/adapter | Talks to persistence or external systems. |
| DTO/contract | Defines what the outside world may send or receive. |
Concrete Spring example
@PostMapping(value = "/api/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
FileResponse upload(@RequestPart MultipartFile file) {
if (file.getSize() > maxUploadBytes) {
throw new FileTooLargeException();
}
return fileService.store(file);
}
The code is intentionally small because the important part is the boundary. The controller should not quietly decide business rules that belong in the service. The service should not depend on servlet objects. The repository should not know which JSON field name the frontend expects. When the boundary is clean, the same behavior can be documented, tested, and changed without touching every layer.
Why it matters
In a demo project, many shortcuts look harmless. Returning all records is fine when there are five rows. Returning an entity is fine until it contains a hidden field. Logging without a request id is fine until several users fail at the same time. Running against a manually created database is fine until staging has a different schema. Production work is mostly about removing these hidden assumptions before they become incidents.
The simplest useful rule is this: make the behavior explicit at the edge, enforce it in the right layer, and keep the public contract stable. If the frontend knows the request format, response format, and error behavior, it can work confidently. If tests exercise the same contract, refactoring becomes safer. If logs and configuration reflect the same design, operations become less dependent on guesswork.
Common mistakes
- Putting the whole use case into a controller method.
- Letting persistence details leak into the API contract.
- Handling the happy path but leaving failure behavior undefined.
- Using a local shortcut that cannot work in staging or production.
Glossary
- bean
- An object managed by the Spring container.
- IoC
- Inversion of Control, a design principle where a framework manages object creation and wiring.
- dependency injection
- A technique where required dependencies are provided to an object from the outside.
- application context
- The Spring container that stores beans, configuration, and runtime wiring.
- auto-configuration
- Spring Boot behavior that configures application components based on dependencies and settings.
- starter
- A Spring Boot dependency bundle that brings related libraries and default configuration together.
- dependency management
- The practice of controlling dependency versions and relationships in a project.
- controller
- A component that handles incoming requests and returns responses or view models.
- service layer
- The application layer where business logic and use-case orchestration usually live.
- repository layer
- The application layer responsible for data access and persistence operations.
- REST controller
- A controller that exposes HTTP endpoints and usually returns JSON or another response body.
- endpoint
- A specific API address and operation that clients can call.
- request mapping
- The configuration that connects an HTTP request path and method to controller code.
- Spring Security
- The Spring project used to implement authentication, authorization, and security filters.
- authentication
- The process of proving who a user or client is.
- authorization
- The process of deciding what an authenticated user or client is allowed to do.
- filter chain
- An ordered set of web filters that process a request before it reaches the application handler.
- observability
- The ability to understand system behavior from signals such as logs, metrics, traces, and health checks.