[Fixed] CSV uploading as Multipart file throws "Failed to perform cleanup of multipart items error in Spring API – Java

by
Ali Hasan
cucumber-java opencsv spring-boot

The Solutions:

Solution 1: Use try-with-resource

The exception you are facing, "Failed to perform cleanup of multipart items," is caused because the Reader object used to read the CSV file is not closed properly. To fix this, you can use a try-with-resource statement, which automatically closes the resources (in this case, the Reader) when the try block exits.

Here’s the updated service class:

try (Reader reader = new InputStreamReader(file.getInputStream())) {
    CSVReader csvReader = new CSVReaderBuilder(reader).withSkipLines(1).build();
    try {
        List<String[]> rows = csvReader.readAll();
        for (String[] arr : rows) {
            for (String temp : arr)
                System.out.print(temp + ", ");
            System.out.println("");
        }
    } catch (IOException | CsvException e) {
        e.printStackTrace();
        throw new MvAppException();
    }
}

Solution:

Convert multipart to File first.

private File convertMultipartToFile(MultipartFile imageFile) throws IOException {
    String file = imageFile.getOriginalFilename();
    log.info("File name ==> {}", file);
    assert file != null;
    File convertFile = new File(file);
    FileOutputStream fileOutputStream = new FileOutputStream(convertFile);
    fileOutputStream.write(imageFile.getBytes());
    fileOutputStream.close();
    return convertFile;
}

then call the convertFile.delete() method after your upload.

Solution 3: Use another option in your given resource file that reference check following code: {solutuonCommits but }
“`

Q&A

CSV uploading as Multipart file throws "Failed to perform cleanup of multipart items error in Spring API".

Use try with resource to fix issue.

How to handle the exception of error in terminal but image is uploaded in db.

use try and catch block to handle the exception.

Video Explanation:

The following video, titled "fix postman error: Couldn't upload file - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Couldn't upload file Make sure that Postman can read files inside the working directory. If you find this video helpful, please consider ...