eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – Diagrid – NPI EA (cat= Testing)
announcement - icon

In distributed systems, managing multi-step processes (e.g., validating a driver, calculating fares, notifying users) can be difficult. We need to manage state, scattered retry logic, and maintain context when services fail.

Dapr Workflows solves this via Durable Execution which includes automatic state persistence, replaying workflows after failures and built-in resilience through retries, timeouts and error handling.

In this tutorial, we'll see how to orchestrate a multi-step flow for a ride-hailing application by integrating Dapr Workflows and Spring Boot:

>> Dapr Workflows With PubSub

1. Overview

In modern web applications, efficiently transferring large files is crucial. Whether we’re sending multiple files to a client or receiving large uploads, we must minimize memory usage. However, Spring’s default buffered approach can bottleneck large payloads. It stores the entire file in memory or on disk before our code processes it. As a result, the application delays processing and consumes more resources.

Fortunately, Spring allows sequential streaming to avoid these limitations. This tutorial explains how to implement streaming for multipart data. Specifically, we discuss Spring MVC and Reactive WebFlux, with practical examples for uploads and downloads.

2. Default Multipart Handling in Spring

A MultipartResolver usually handles multipart requests in Spring MVC. It parses each incoming file and temporarily stores it in memory or on disk before passing it to the controller. Similarly, the default approach often loads the entire response into memory before sending it to a client.

While this method is straightforward and works for small files, it presents two major issues with larger uploads or downloads:

  • High memory consumption: Large files can cause our applications to use excessive memory, which may result in slow performance or even an OutOfMemoryError.
  • Delayed processing or delivery: The application must wait until all parts of the request are fully received before starting any processing or sending data, which postpones the first byte reaching the client.

These limitations make the default method unsuitable for large archives, massive datasets, or real-time uploads. A streaming approach solves the problem by processing or sending data as it arrives, without waiting for the full payload.

3. Streaming in Spring MVC

In Spring MVC applications, streaming enables us to send or receive large files incrementally, rather than buffering them entirely in memory or on disk. This approach keeps memory usage predictable, reduces latency, and enables real-time processing.

We’ll first examine streaming file uploads, then streaming file downloads, exploring both configuration and implementation techniques for each scenario.

3.1. Streaming File Uploads

In this approach, the application can process data immediately as it arrives, enabling early validation, transformation, or persistence. This ensures predictable memory usage even with multi-gigabyte uploads.

The first step is to configure the MultipartResolver to minimize buffering. Setting the file-size threshold to 0 in application.properties ensures that uploaded files are streamed directly from the request rather than being buffered in memory:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.file-size-threshold=0

Setting spring.servlet.multipart.file-size-threshold=0 disables in-memory buffering for all files. Any uploaded file, regardless of size, will be written directly to disk or processed as a stream instead of being held in memory. This setting is essential for predictable memory usage when handling large files, as it prevents sudden spikes in heap usage and allows the application to begin processing data immediately upon receipt.

With this configuration in place, controllers can receive uploaded files as instances of MultipartFile and process them incrementally:

@PostMapping("/upload")
public ResponseEntity<String> uploadFileStreaming(@RequestPart("filePart") MultipartFile filePart) throws IOException {
    Path targetPath = UPLOAD_DIR.resolve(filePart.getOriginalFilename());
    Files.createDirectories(targetPath.getParent());
    try (InputStream inputStream = filePart.getInputStream(); OutputStream outputStream = Files.newOutputStream(targetPath)) {
        inputStream.transferTo(outputStream);
    }
    return ResponseEntity.ok("Upload successful: " + filePart.getOriginalFilename());
}

Because the file data is read as a stream from the MultipartFile, this approach avoids buffering the entire upload in memory. The transferTo() method efficiently copies the input stream to the output stream in a memory-conscious manner. This allows the controller to process large files incrementally, keeping memory usage predictable and making it straightforward to integrate streaming uploads into existing Spring MVC controllers.

3.2. Streaming File Downloads

Spring MVC’s default behavior buffers entire responses before sending them, wasting memory and delaying delivery for large payloads. The StreamingResponseBody API solves this by writing directly to the response output stream, allowing the first file to be sent while subsequent files are still being processed.

For multiple files in a single HTTP response, we can use the multipart/mixed content type with a boundary string to separate each file in the stream:

@GetMapping("/download")
public StreamingResponseBody downloadFiles(HttpServletResponse response) throws IOException {
    String boundary = "filesBoundary";
    response.setContentType("multipart/mixed; boundary=" + boundary);
    List<Path> files = List.of(UPLOAD_DIR.resolve("file1.txt"), UPLOAD_DIR.resolve("file2.txt"));
    return outputStream -> {
        try (BufferedOutputStream bos = new BufferedOutputStream(outputStream); OutputStreamWriter writer = new OutputStreamWriter(bos)) {
            for (Path file : files) {
                writer.write("--" + boundary + "\r\n");
                writer.write("Content-Type: application/octet-stream\r\n");
                writer.write("Content-Disposition: attachment; filename=\"" + file.getFileName() + "\"\r\n\r\n");
                writer.flush();
                Files.copy(file, bos);
                bos.write("\r\n".getBytes());
                bos.flush();
            }
            writer.write("--" + boundary + "--\r\n");
            writer.flush();
        }
    };
}

In this example, each file is streamed directly from disk to the output stream. The explicit boundary markers allow the client to parse the stream into distinct files, and flushing after each write ensures that the data is pushed to the client without unnecessary delay. This method keeps memory use low and improves perceived performance, as users begin receiving data as soon as it’s available.

4. Reactive Streaming With WebFlux

While Spring MVC efficiently streams files, Spring WebFlux provides superior scalability through non-blocking, backpressure-aware data handling. It streams files without blocking threads or excessive memory consumption. Although the core sequential streaming concepts remain, WebFlux implements them using reactive types like Flux and Mono instead of InputStream and OutputStream.

4.1. Streaming File Uploads

In WebFlux, we handle uploads by processing the multipart request as a reactive stream of Part objects. The key is to use the native FilePart interface, which provides the file content as a Flux<DataBuffer>. This allows us to process data chunks as they arrive over the network and write them to their destination using non-blocking I/O operations, maintaining the reactive chain from the network socket all the way to the disk:

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public Mono<String> uploadFileStreaming(@RequestPart("filePart") FilePart filePart) {
    return Mono.fromCallable(() -> {
        Path targetPath = UPLOAD_DIR.resolve(filePart.filename());
        Files.createDirectories(targetPath.getParent());
        return targetPath;
    }).flatMap(targetPath ->
      filePart.transferTo(targetPath)
        .thenReturn("Upload successful: " + filePart.filename())
    );
}

This creates a non-blocking pipeline where FilePart.transferTo() internally handles the reactive streaming from request to the filesystem. The process is backpressure-aware, automatically regulating data flow to match disk speed and prevent server overload.

4.2. Streaming File Downloads

For downloads, WebFlux allows us to return the file content as a Flux<DataBuffer>, which Spring writes directly to the HTTP response socket. This approach streams the file to the client incrementally, without ever loading the entire content into memory. It’s the reactive equivalent of MVC’s StreamingResponseBody and is incredibly efficient for serving large assets:

@GetMapping(value = "/download", produces = "multipart/mixed")
public ResponseEntity<Flux<DataBuffer>> downloadFiles() {
    String boundary = "filesBoundary";

    List<Path> files = List.of(
      UPLOAD_DIR.resolve("file1.txt"),
      UPLOAD_DIR.resolve("file2.txt")
    );

    // Use concatMap to ensure files are streamed one after another, sequentially.
    Flux<DataBuffer> fileFlux = Flux.fromIterable(files)
      .concatMap(file -> {
          String partHeader = "--" + boundary + "\r\n" +
            "Content-Type: application/octet-stream\r\n" +
            "Content-Disposition: attachment; filename=\"" + file.getFileName() + "\"\r\n\r\n";

          Flux<DataBuffer> fileContentFlux = DataBufferUtils.read(file, new DefaultDataBufferFactory(), 4096);
          DataBuffer footerBuffer = new DefaultDataBufferFactory().wrap("\r\n".getBytes());

          // Build the flux for this specific part: header + content + footer
          return Flux.concat(
            Flux.just(new DefaultDataBufferFactory().wrap(partHeader.getBytes())),
            fileContentFlux,
            Flux.just(footerBuffer)
          );
      })
      // After all parts, concat the final boundary
      .concatWith(Flux.just(
        new DefaultDataBufferFactory().wrap(("--" + boundary + "--\r\n").getBytes())
      ));

    return ResponseEntity.ok()
      .header(HttpHeaders.CONTENT_TYPE, "multipart/mixed; boundary=" + boundary)
      .body(fileFlux);
}

Crucially, concatMap() ensures truly sequential streaming by processing one file’s entire Flux before starting the next, preserving the multipart order. This is combined with the efficiency of DataBufferUtils.read(), which streams file content in 4KB chunks using non-blocking I/O. The result is that the entire file is never loaded into memory, clients receive data immediately, and memory usage remains minimal.

5. Conclusion

Sequential streaming in Spring lets us handle large file transfers without draining memory or delaying processing. Whether we use StreamingResponseBody in MVC or Flux<Part> in WebFlux, the key is to process data as it arrives.

For small files, the default buffered approach works fine. But when we deal with multi-GB datasets, large archives, or real-time uploads, streaming gives us lower latency, predictable memory use, and better scalability.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)