Receive a Large file in chunks and process it as its received in spring-boot

I wanted to process a CSV file which might have millions of records in it.

I wanted to process it as its received in chunks. (process the file contents even before the full file uploaded , process in parts)
I was able to analyze how it can be done for the spring-boot-starter-webflux.
but my application is spring-boot-starter-web.

My implementation for the spring-starter-webflux.

@PostMapping("/upload")
public Mono<Void> uploadFile2(@RequestPart("file") FilePart filePart) {
    StringBuilder accumulator = new StringBuilder();

    return filePart.content()
            .doOnNext(dataBuffer -> {
                System.out.println("------------------------ Processing started ----------------------------");
            })
            .map(dataBuffer -> {
                byte[] bytes = new byte[dataBuffer.readableByteCount()];
                dataBuffer.read(bytes);
                return new String(bytes, StandardCharsets.UTF_8);
            })
            .flatMap(content -> {
                accumulator.append(content);
                String accumulatedContent = accumulator.toString();
                String[] lines = accumulatedContent.split("n");

                // Process all complete lines
                for (int i = 0; i <= lines.length - 1; i++) {
                    System.out.println("Processing line: " + lines[i]);
                }

                // Check if the last character of the accumulated content is a newline
                if (accumulatedContent.charAt(accumulatedContent.length() - 1) == 'n') {
                    // The last line is complete
                    accumulator.setLength(0); // Clear the accumulator
                } else {
                    // The last line is incomplete, keep it in the accumulator
                    accumulator.setLength(0);
                    accumulator.append(lines[lines.length - 1]);
                    System.out.println("last line incomplete - " + accumulator);
                }

                return Mono.empty();
            })
            .then(Mono.defer(() -> {
                // Process any remaining content in the accumulator
                if (accumulator.length() > 0) {
                    System.out.println("Processing last line: " + accumulator.toString());
                }
                return Mono.empty();
            }));
}

How can i implement this same functionality in spring-boot-starter-web.

I tried using apache-commons-fileupload.

The implementation for the apache commons-fileupload in spring-boot-starter-web:

  @PostMapping("/upload")
public String handleFileUpload(HttpServletRequest request) {

    System.out.println("-------------------------- Start ---------------------");
    if (!JakartaServletFileUpload.isMultipartContent(request)) {
        return "Error: Form must have enctype=multipart/form-data.";
    }

    JakartaServletFileUpload upload = new JakartaServletFileUpload();

    try {
        var iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            System.out.println("--------------------- CHUNK --------------------");
            FileItemInput item = iter.next();
            String name = item.getFieldName();
            InputStream stream = item.getInputStream();
            if (item.isFormField()) {
                System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
            } else {
                System.out.println("File field " + name + " with file name " + item.getName() + " detected.");

                // Read InputStream into a byte array
                byte[] fileBytes = stream.readAllBytes();

                // Save the file
                File destinationFile = new File("C:\Users\anon\Desktop\output\"
                        + new Random().nextInt(100000) + ".csv");
                try (FileOutputStream fos = new FileOutputStream(destinationFile)) {
                    fos.write(fileBytes);
                }

                // Create a new InputStream from the byte array
                try (InputStream newStream = new ByteArrayInputStream(fileBytes)) {
                    System.out.println(readFromInputStream(newStream));
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        return "Error: " + e.getMessage();
    }

    return "File uploaded successfully.";
}

public static String readFromInputStream(InputStream inputStream) {
    System.out.println("--------------- processing chunk -------------");
    StringBuilder resultStringBuilder = new StringBuilder();
    try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
        String line;
        while ((line = br.readLine()) != null) {
            resultStringBuilder.append(line).append("n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return resultStringBuilder.toString();
}

but this is not receiving the file in chunks.
Any help on how to do it in Spring-boot-starter-web would be helpful.

Thanks in advance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật