Quick revision
The short answer
With Jakarta REST 3.1 we write a @POST method with @Consumes(MediaType.MULTIPART_FORM_DATA) that takes @FormParam("file") EntityPart file and @FormParam("description") String description, or a List<EntityPart> of all parts. We stream file.getContent() straight to storage through a counting stream that enforces a size limit, sniff the real content type from the first bytes, store the file under a generated id rather than the client's file name, and return 201 with the stored file's URI. Older code used Jersey's @FormDataParam with FormDataContentDisposition or RESTEasy's MultipartFormDataInput, each needing its multipart module.
What to remember
In this answer, we wrote a Jakarta REST 3.1 upload endpoint that takes the file as an EntityPart and the description as a String through @FormParam, streams the content to storage through a bounded stream, checks the real type from magic bytes and stores the file under a generated id. We enforced size limits at the proxy and in the stream, avoided path traversal by never using the client's file name for paths, returned 201 with the new URI, and compared the standard API with the older Jersey and RESTEasy ones.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With Jakarta REST 3.1 we write a @POST method with @Consumes(MediaType.MULTIPART_FORM_DATA) that takes @FormParam("file") EntityPart file and @FormParam("description") String description, or a List<EntityPart> of all parts. We stream file.getContent() straight to storage through a counting stream that enforces a size limit, sniff the real content type from the first bytes, store the file under a generated id rather than the client's file name, and return 201 with the stored file's URI. Older code used Jersey's @FormDataParam with FormDataContentDisposition or RESTEasy's MultipartFormDataInput, each needing its multipart module.
Why this question is asked
File upload is where many APIs get their first serious security bug or their first out-of-memory crash. Before Jakarta REST 3.1, every runtime had its own multipart API, so code was tied to Jersey or RESTEasy; now there is a standard. Interviewers ask this to check both the API and the judgement: streaming instead of buffering, size limits, and never trusting the client's file name. In this answer, we will recap what a multipart request looks like, write the endpoint with EntityPart, add the limits, compare with the older vendor-specific APIs and answer where large parts are buffered.
1A Quick Recap of multipart/form-data
When a browser form uploads a file, it sends a body of type multipart/form-data. The body is split into parts separated by a boundary string. Each part has headers, most importantly Content-Disposition, which carries the field name and, for files, the file name the user chose, and optionally a Content-Type. So our request has two parts: one named file with the bytes of, say, a PDF, and one named description with plain text.
Jakarta REST 3.1, part of Jakarta EE 10, added the EntityPart interface to represent one such part in a standard way. It exposes the part's name, its optional file name, its media type, its headers and its content as a stream or converted to a Java type.
2Writing the Endpoint
Here is the resource. DocumentStore is our service that writes to disk or object storage from an InputStream.
Notice that we never call readAllBytes. The content flows from the request through our bounded stream into storage, so a 20 MB file uses a small buffer, not 20 MB of heap per request. The description arrives as a String because it is small text; the runtime converts that part for us.
3Enforcing the Limits
A size limit needs two layers. The first is outside the application: the reverse proxy and the server refuse request bodies over a maximum, for example 25 MB, so a 10 GB upload is cut off before it reaches our code. The second is inside: BoundedInputStream is a small wrapper we write that counts bytes as they are read and throws an exception mapped to 413 Payload Too Large once the count passes MAX_BYTES. We need both, because proxy limits apply to the whole body and can be misconfigured, while the stream limit applies exactly to the file part.
The content type the client sends in the part header is just a claim, so we check the actual bytes. A PDF starts with %PDF-, and a PNG starts with a fixed 8-byte signature. ContentSniffer reads the first few bytes, compares them with the allowed signatures and resets the stream, or we can use a library such as Apache Tika. If uploaded files will be shared with other users, we also send them through a virus scanner before they become visible.
- Proxy body limitOver 25 MB: 413 before the app
- Bounded stream while readingOver 20 MB: 413
- Magic bytes match an allowed type?YesContinueNo415 Unsupported Media Type
- Store under a generated idClient file name kept only as metadata
- Optional virus scanVisible to others only when clean
- 201 with LocationURI of the stored document
6 steps in order. Play them, or step through them one at a time.
4Never Trusting the Client's File Name
The file name in Content-Disposition is chosen by the client, and it can be anything, including ../../etc/cron.d/job or a name with control characters. If we build a storage path from it, we have a path traversal vulnerability: the file is written outside the upload folder. So we store the file under an id we generated, such as a UUID, and keep the original name only as display metadata after sanitising it, which means taking only the last path segment, removing control characters and limiting its length.
This also solves collisions: two users uploading report.pdf no longer overwrite each other.
5Comparing With the Older Vendor APIs
Before 3.1, each runtime had its own way. Jersey uses @FormDataParam("file") InputStream with FormDataContentDisposition for the file name, and needs the jersey-media-multipart module and MultiPartFeature registered. RESTEasy uses MultipartFormDataInput, from which we get parts by name, and needs its multipart provider module. Both still work, and much existing code uses them, but code written against EntityPart and @FormParam is portable across runtimes that implement Jakarta REST 3.1 or later. Some runtimes still need their multipart module present to implement EntityPart, so check your version.
6Follow-up questions
Where are large parts buffered while the request is parsed?
It depends on the runtime. A multipart body is a single stream, so to give us a later part, the parser must read past earlier ones, and implementations typically keep small parts in memory and write larger ones to temporary files beyond a size threshold. Jersey's multipart support and RESTEasy's parser both do something like this, with configurable thresholds and temp directories. So even with streaming code, the temp directory needs space and cleanup, and we should check the runtime's settings. Putting the file part last, or using List<EntityPart> and processing parts in order, reduces buffering.
Why return 201 rather than 200?
Because the upload created a new resource. 201 Created with a Location header tells the client exactly where the stored document lives, so it can link to or download it without guessing.
How would you accept several files in one request?
Take List<EntityPart> parts, iterate in order, and handle each part by name. The same per-file size limit applies, plus a limit on the number of parts and the total size.
7Conclusion
In this answer, we wrote a Jakarta REST 3.1 upload endpoint that takes the file as an EntityPart and the description as a String through @FormParam, streams the content to storage through a bounded stream, checks the real type from magic bytes and stores the file under a generated id. We enforced size limits at the proxy and in the stream, avoided path traversal by never using the client's file name for paths, returned 201 with the new URI, and compared the standard API with the older Jersey and RESTEasy ones.
Open with
With Jakarta REST 3.1 there is now a standard multipart API, so I would write a @POST method with @Consumes multipart/form-data and two parameters: @FormParam("file") EntityPart file and @FormParam("description") String description.
Then, if they want more
Alternatively I could take a List of EntityPart and process the parts in order. For the file, I would read file.getContent() as a stream and pass it straight to the storage service, never readAllBytes, so memory stays flat whatever the size.
I would wrap the stream in a counting stream that throws once it passes, say, 20 MB, and map that to 413, and I would also set a body limit at the proxy and server, because both layers are needed.
I would check the real content type by sniffing the magic bytes, like %PDF- for a PDF, instead of trusting the part's Content-Type header, and reject anything else with 415. If files are shared with other users, they go through a virus scan before becoming visible.
I would store the file under a generated id and keep the client's file name only as sanitised display metadata, because using it in a path is a path traversal bug. The response is 201 with a Location built from UriInfo. In older code you see Jersey's @FormDataParam with FormDataContentDisposition, or RESTEasy's MultipartFormDataInput, and both need their multipart modules registered.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Jakarta EE 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a JAX-RS endpoint that accepts a multipart/form-data upload with a file part named...": https://scrivio-interview-library.netlify.app/java/jax-rs/6de6e390dc/