Skip to content
Ameen Abozaid
JNR-023

File operations

  • 6 Lessons
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Multipart uploads
  • downloads
  • streaming
  • file-size restrictions

Learn first

2 h of practice (estimate)

Learning outcomes

  1. Accept multipart uploads and enforce size and content-type limits.
  2. Stream downloads without loading whole files into memory.
  3. Store uploaded files outside the application with generated names.
  4. Explain upload-related risks (path traversal, oversized files, content sniffing).

Lessons

  1. 01Multipart uploads
  2. 02Limits and validation
  3. 03Streaming downloads
  4. 04Where to store files
  5. 05Upload security
  6. 06Demo: product image upload and streaming download

Lab and exercises

Lab

JNR-023-LAB1 · Product images

Outcomes: 1–4 · Time: ~2 h Upload, list, and download product images in Shop API. Enforce a size limit and a content-type allow-list; store files in a configured directory outside the application using generated names; stream downloads. Complete the security checklist below.

Security checklist: filename never used in a path; size limit enforced; content type allow-listed; files outside the JAR; nosniff header set; downloads only through the controller.

Acceptance: path traversal attempt rejected; 20 MB file rejected.

Exercises (assessment: lab plus security checklist)

IDOutcomeTask
JNR-023-EX011Implement a multipart endpoint and upload a file with curl -F.
JNR-023-EX021Configure a 2 MB limit and show the response for a 3 MB file.
JNR-023-EX031Reject files whose content type is not image/png or image/jpeg, and empty files.
JNR-023-EX042Return a stored file as a Resource with the right Content-Type, and 404 when missing.
JNR-023-EX053Store uploads in a configured external directory using UUID names, with the original name kept as metadata.
JNR-023-EX064Explain path traversal, oversized files, and content sniffing, each with one prevention.
Show solutions
  • EX01 curl -i -F "file=@pen.png" localhost:8080/products/1/images.
  • EX02 spring.servlet.multipart.max-file-size=2MB; the response is an error status (413 or a mapped one) with a clear body.
  • EX03 Set.of("image/png","image/jpeg").contains(file.getContentType()); file.isEmpty() gives 400.
  • EX04 new FileSystemResource(path) in ResponseEntity.ok().contentType(...).body(resource); missing record gives 404.
  • EX05 String stored = UUID.randomUUID().toString(); Files.copy(file.getInputStream(), base.resolve(stored));
  • EX06 Path traversal: never use the client name, normalize and check startsWith(base). Oversized files: size limits. Sniffing: correct Content-Type plus X-Content-Type-Options: nosniff.

Outcome 4 is also covered by the security checklist and quiz Q08 to Q10.

Quiz

Outcomes: O1 uploads and limits · O2 streaming downloads · O3 storage · O4 upload risks.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1File uploads use which content type?O1
  2. 2A Spring type for an uploaded file is:O1
  3. 3Why check content type and size?O1
  4. 4Streaming a download avoids:O2
  5. 5Which return type suits a file download?O2
  6. 6Where should uploads be stored?O3
  7. 7Who chooses the stored filename?O3
  8. 8A filename ../../secret.txt is an example of:O4
  9. 9Best defense against that attack:O4
  10. 10X-Content-Type-Options: nosniff helps against:O4

Practical part

Six exercises JNR-023-EX01 to EX06, the product-images lab, and the security checklist. Pass: at least five exercises and a lab that rejects the traversal attempt and the 20 MB file.

Common mistakes

Trusting the client's filename or content type; storing files inside the JAR's directory.

Assessment

Lab plus security checklist.

Career relevance

Object storage (MID-056, ARC-015).

Type to search levels, phases, modules and lessons.