HTTP
Files, streaming and SSE
Uploads, downloads, server-sent events and static files.
- Uploads:
@FormFilebinds multipart parts, with a size limit (below). Files are held in memory, so this is not suited to very large files.FileValidationPipealso checks type. - Downloads: return a
StreamableFile. - Server-sent events: return an
SseStreamand push events to it. - Static files:
serveStaticserves a directory.
Uploads and the size limit
An upload is a multipart/form-data request. Each file part is bound to a parameter of type UploadedFile (filename, contentType, bytes, size):
@Controller("/avatars")
class AvatarController(private val storage: StorageService) {
@Post("/{id}")
fun upload(@Param("id") id: String, @FormFile("file") file: UploadedFile): Map<String, Any> { // limit: 2 MB, the default
storage.put("avatars/$id", file.bytes, file.contentType)
return mapOf("size" to file.size)
}
@Post("/cover")
fun cover(@FormFile("image", maxSize = 10 * 1024 * 1024) image: UploadedFile) = ... // this one allows 10 MB
@Post("/raw")
fun raw(@FormFile("data", maxSize = -1) data: UploadedFile) = ... // -1: no limit (the engine's body limit still applies)
@Post("/gallery")
fun gallery(@FormFiles("photos", maxSize = 5 * 1024 * 1024) photos: List<UploadedFile>) = ... // the limit applies to EACH file
}maxSize is in bytes. When it is not given it is 2 MB. A file that is too big is not passed to your method: Kest throws FileSizeExceededException (it carries field, filename, size and maxSize). A file exactly at the limit is accepted.
What the client gets. If nothing handles the exception it is a BadRequestException, so the answer is 400 with the message File 'photo.png' exceeds the maximum size of 2 MB (it is 3.4 MB). To shape the answer yourself, catch it like any exception, with a filter or a controller advice (whichever your application uses):
@ControllerAdvice
class UploadErrors {
@ExceptionHandler @ResponseStatus(413)
fun tooBig(e: FileSizeExceededException) = mapOf("error" to "file_too_large", "field" to e.field, "maxBytes" to e.maxSize)
}
@Catch(FileSizeExceededException::class)
class TooBigFilter : ExceptionFilter {
override suspend fun catch(exception: Throwable, ctx: ExecutionContext) =
HttpResponse(413, mapOf("content-type" to "application/json"), """{"error":"file_too_large"}""".toByteArray())
}Two limits, two jobs. The engine also has a limit on the whole request body (NettyHttpAdapter(maxBodyBytes = ...), 10 MB by default), which protects the server before any parsing; a bigger request is refused with 413. The per-file maxSize is the rule for each upload and should be at most the body limit. If you allow 50 MB files, raise maxBodyBytes as well.
Do not trust the file's type from the client: check contentType (and ideally the file's first bytes) before you store or serve it. FileValidationPipe(maxBytes, allowedTypes) can do this for a whole application.
@Get("/events")
fun events() = SseStream { out ->
repeat(10) { out.send("tick $it"); Thread.sleep(1000) }
}