I assume that you want the user to upload the file from some UI. Depending on the exact way in which you build UI, you might:

  • Send a multipart HTTP POST request (mime type = multipart/form-data; see What should a Multipart HTTP request with multiple files look like?)
  • Send a simple POST request with the body directly containing the file contents.

Either of the two can be fairly easily solved using Spring.

Assuming that we have the following entity:

@Data
@Entity
public class User {
    @Id
    private String username;
    private String phoneNumber;
    private String address;
}

And we define a Spring Data repository for accessing the database:

public interface UserRepository extends JpaRepository<User, String> {

}

For the CSV deserialization, I would propose using Jackson. Spring Boot already comes with Jackson, but we need to add a data format extension for CSV in your pom:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
    </dependency>

This way, we can create a simple utility method that knows to read a CSV for a given POJO class:

public class CsvUtils {
    private static final CsvMapper mapper = new CsvMapper();
    public static <T> List<T> read(Class<T> clazz, InputStream stream) throws IOException {
        CsvSchema schema = mapper.schemaFor(clazz).withHeader().withColumnReordering(true);
        ObjectReader reader = mapper.readerFor(clazz).with(schema);
        return reader.<T>readValues(stream).readAll();
    }
}

And then we create a simple Rest Controller for handling the upload(s):

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserRepository repository;

    @PostMapping(value = "/upload", consumes = "text/csv")
    public void uploadSimple(@RequestBody InputStream body) {
        repository.saveAll(CsvUtils.read(User.class, body));
    }

    @PostMapping(value = "/upload", consumes = "multipart/form-data")
    public void uploadMultipart(@RequestParam("file") MultipartFile file) {
        repository.saveAll(CsvUtils.read(User.class, file.getInputStream()));
    }
}

In case you also need some HTML for doing the upload, the following snippet is a minimal working example:

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="Submit" />
</form>

Later edit: If you want to also validate the incoming data, first annotate your entity class attribute with javax.validation constraints. For example:

@Data
@Entity
class User {
    @Id
    @Email
    @NotEmpty
    private String username;
    @Pattern(regexp = "[0-9 ()-]{4,12}")
    private String phoneNumber;
    private String address;
}

Then you can chose where do perform the actual validation call:

  1. Service level. This is what I personally recommend in this case, as it is fairly easy to setup and would perform the validations early enough. For this you introduce a simple @Service class between the controller and the repository.
@Service
@Validated
@RequiredArgsConstructor
class UserService {
    private final UserRepository repository;
    public void saveAll(@Valid List<User> users) {
        repository.saveAll(users);
    }
}

You would then use this service class instead of the repository inside the controller class.

  1. Repository level: here you don't actually need to do anything. If you annotate your entity classes with validation constraints, Hibernate would automatically call the validation in a pre-insert listener (BeanValidationEventListener).

  2. Controller level. This is trickier to setup. Move the CSV deserialization in a custom HttpMessageConverter. You should also add this converter to the FormHttpMessageConverter (such that it can use it to deserialize a part of the multi-part request). You could then theoretically just declare the @Valid List<User> as inputs for your controller methods and Spring would automatically call the message converter based on the mime type and then call the validator. See Add JSON message converter for multipart/form-data for an example.

Lastly you can always manually call the validation whenever you want: Manually call Spring Annotation Validation.

Answer from Serban Petrescu on Stack Overflow
Top answer
1 of 1
47

I assume that you want the user to upload the file from some UI. Depending on the exact way in which you build UI, you might:

  • Send a multipart HTTP POST request (mime type = multipart/form-data; see What should a Multipart HTTP request with multiple files look like?)
  • Send a simple POST request with the body directly containing the file contents.

Either of the two can be fairly easily solved using Spring.

Assuming that we have the following entity:

@Data
@Entity
public class User {
    @Id
    private String username;
    private String phoneNumber;
    private String address;
}

And we define a Spring Data repository for accessing the database:

public interface UserRepository extends JpaRepository<User, String> {

}

For the CSV deserialization, I would propose using Jackson. Spring Boot already comes with Jackson, but we need to add a data format extension for CSV in your pom:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-csv</artifactId>
    </dependency>

This way, we can create a simple utility method that knows to read a CSV for a given POJO class:

public class CsvUtils {
    private static final CsvMapper mapper = new CsvMapper();
    public static <T> List<T> read(Class<T> clazz, InputStream stream) throws IOException {
        CsvSchema schema = mapper.schemaFor(clazz).withHeader().withColumnReordering(true);
        ObjectReader reader = mapper.readerFor(clazz).with(schema);
        return reader.<T>readValues(stream).readAll();
    }
}

And then we create a simple Rest Controller for handling the upload(s):

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserRepository repository;

    @PostMapping(value = "/upload", consumes = "text/csv")
    public void uploadSimple(@RequestBody InputStream body) {
        repository.saveAll(CsvUtils.read(User.class, body));
    }

    @PostMapping(value = "/upload", consumes = "multipart/form-data")
    public void uploadMultipart(@RequestParam("file") MultipartFile file) {
        repository.saveAll(CsvUtils.read(User.class, file.getInputStream()));
    }
}

In case you also need some HTML for doing the upload, the following snippet is a minimal working example:

<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <input type="submit" name="submit" value="Submit" />
</form>

Later edit: If you want to also validate the incoming data, first annotate your entity class attribute with javax.validation constraints. For example:

@Data
@Entity
class User {
    @Id
    @Email
    @NotEmpty
    private String username;
    @Pattern(regexp = "[0-9 ()-]{4,12}")
    private String phoneNumber;
    private String address;
}

Then you can chose where do perform the actual validation call:

  1. Service level. This is what I personally recommend in this case, as it is fairly easy to setup and would perform the validations early enough. For this you introduce a simple @Service class between the controller and the repository.
@Service
@Validated
@RequiredArgsConstructor
class UserService {
    private final UserRepository repository;
    public void saveAll(@Valid List<User> users) {
        repository.saveAll(users);
    }
}

You would then use this service class instead of the repository inside the controller class.

  1. Repository level: here you don't actually need to do anything. If you annotate your entity classes with validation constraints, Hibernate would automatically call the validation in a pre-insert listener (BeanValidationEventListener).

  2. Controller level. This is trickier to setup. Move the CSV deserialization in a custom HttpMessageConverter. You should also add this converter to the FormHttpMessageConverter (such that it can use it to deserialize a part of the multi-part request). You could then theoretically just declare the @Valid List<User> as inputs for your controller methods and Spring would automatically call the message converter based on the mime type and then call the validator. See Add JSON message converter for multipart/form-data for an example.

Lastly you can always manually call the validation whenever you want: Manually call Spring Annotation Validation.

🌐
Baeldung
baeldung.com › home › java › java io › reading a csv file into an array
Reading a CSV File into an Array | Baeldung
May 9, 2025 - Furthermore, to parse the CSV file, we need to make the delimiter the same as in the CSV file: public static final String COMMA_DELIMITER = "\\|"; Let’s note that we need to escape special characters in a Java regular expression.
Discussions

Spring Boot Application Can't Find CSV File
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
2
1
November 28, 2023
java - Reading a csv file with Spring Boot application - Stack Overflow
I am trying to convert a Java program I had written to read a csv file into a Spring Boot Application but keep getting a NullPointerException. I just want to print out the contents of the csv file. More on stackoverflow.com
🌐 stackoverflow.com
May 26, 2017
How to read data from CSV file in Spring Boot or Java? - Stack Overflow
I am trying to read data from CSV in my application. I have exported table data as CSV from MySql database. So that generated csv file, trying to read. MySql has generated CSV file in different for... More on stackoverflow.com
🌐 stackoverflow.com
multithreading - Peformance issues reading CSV files in a Java (Spring Boot) application - Stack Overflow
I am currently working on a spring based API which has to transform csv data and to expose them as json. it has to read big CSV files which will contain more than 500 columns and 2.5 millions lines... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
naveenrk22.medium.com › import-wizard-reading-csvs-and-excel-sheets-in-spring-boot-d509b3d5cc1e
Import Wizard — Reading CSVs and Excel Sheets in Spring Boot | by Naveen Kumar Ravi | Medium
August 9, 2023 - Set up a Spring Boot project. Create REST endpoints to accept file uploads. Use OpenCSV to parse CSV data. Use Apache POI to read Excel sheets. Return analysis of file contents.
🌐
Attacomsian
attacomsian.com › blog › spring-boot-upload-parse-csv-file
How to upload and parse a CSV file using Spring Boot
September 24, 2022 - A step-by-step guide to learn how to upload and parse a CSV file in Spring Boot and Thymeleaf.
🌐
BezKoder
bezkoder.com › home › spring boot: upload & read csv file into mysql database | multipart file
Spring Boot: Upload & Read CSV file into MySQL Database | Multipart File - BezKoder
April 4, 2023 - Spring Boot - Upload, read data from CSV file and store in MySQL database table - Spring Rest API that returns CSV file - Spring Upload/Download CSV file
🌐
GitHub
github.com › arisusantolie › Upload-and-Read-CSV-File-Java-In-Spring-Boot
GitHub - arisusantolie/Upload-and-Read-CSV-File-Java-In-Spring-Boot
For Detail Explanation, can be seen on my personal blog at : https://arisusantolie.my.id/upload-and-read-csv-file-java-in-spring-boot/
Author   arisusantolie
🌐
Medium
medium.com › @mohamedhedi.aissi › spring-boot-csv-service-using-opencsv-5afd5c66c125
Populating DataBase in Spring Boot using CSV files and OpenCsv | by Mohamed Hedi Aissi | Medium
June 17, 2024 - The country entity will contain the data for the different countries with id is the 3-letter code for each country. We need to implement CountryRepository.java to save the data read from CSV : public interface CountryRepository extends JpaRepository<Country, String> { } Now Let’s create a service that handles the different operations on CSV files.
Find elsewhere
🌐
MojoAuth
mojoauth.com › parse-and-generate-formats › parse-and-generate-csv-with-spring-boot
Parse and Generate CSV with Spring Boot | Parse and Generate Formats
Spring Boot makes parsing CSV files straightforward by integrating with libraries like opencsv or Jackson's CSV module. The key is mapping CSV rows directly to your Java objects. You can use CsvToBean from opencsv or Jackson's CsvMapper for ...
🌐
Reddit
reddit.com › r/javahelp › spring boot application can't find csv file
r/javahelp on Reddit: Spring Boot Application Can't Find CSV File
November 28, 2023 -

repository for reference: https://github.com/izlemontee/pokemonserver

hey everyone, i'm building a pokemon database on Spring Boot but I can't even get it to start up.

What I intend to do is:

-in the repository, nationalDexRepo under the repo folder, read the Pokemon csv file (found in src/main/resources/static/csv/Pokemon.csv), and parse that info into a Map. I've done the parsing into a Map before when running as a normal java app, but when I try to run it as a Springboot app, it suddenly doesn't work. I included the reading of the CSV file in the constructor of the repo, nationalDexRepo.java to initialise it.

The repo contains these lines:

    final String dirPath = "/csv";
final String csvName = "/Pokemon.csv";

and in the method, readCSV, the path is used:

public void readCSV()throws Exception{
    boolean stop = false;
    String fileString = dirPath+ csvName;
    File f = new File(fileString);
    System.out.println(fileString);
    while(!stop){
        if(f.exists() && f.isFile()){

if the file does NOT exist, it's just a blank code block, and the fileString is "/csv/Pokemon.csv".

I tried different possible permutations of the directory, but still no dice. I'm still pretty new to SpringBoot so I'm not entirely sure if I'm calling the correct directories to access my file using the repo.

Top answer
1 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 2
1
Check ClassPathResource In short, your code looks for files relative to it's working directory. To access files from jar resources, you can use for example class linked above.
🌐
YouTube
youtube.com › code coffee java
How To Read Csv File In Java | Spring Boot | Rest API | Code Coffee Java - YouTube
How To Read Csv File In Java | Spring Boot | Rest API | Code Coffee JavaIn this comprehensive Java programming tutorial, we'll demystify the process of readi...
Published   October 26, 2023
Views   536
🌐
springjava
springjava.com › spring-boot › how-to-import-csv-file-in-spring-boot
How to import CSV file in Spring Boot
March 9, 2024 - To import a CSV file into Spring Boot Application use Apache Commons CSV library to read the content of the file and store it in the Java List. A CSV is a comma-separated value, a simple plain text file containing data with comma separated.
🌐
Stack Overflow
stackoverflow.com › questions › 44154061 › reading-a-csv-file-with-spring-boot-application
java - Reading a csv file with Spring Boot application - Stack Overflow
May 26, 2017 - package sample.spring.chapter01; import java.util.List; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.springframework.core.io.Resource; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class PieceDAO { private List<Piece> listp; //private Resource resource; public PieceDAO() { } public void setlistp(Piece p) throws FileNotFoundException, IOException{ listp=new Arra
🌐
DEV Community
dev.to › loil › upload-and-read-csv-file-in-spring-boot-550c
Upload and Read CSV File in Spring Boot - DEV Community
January 4, 2021 - I use ApacheCommon or Open CSV libraries to parse and read CSV files. I use SpringJPA to save data from CSV files to MySQL and PostgreSQL. I implement a SpringBoot Global Exception Handler when uploading with a very big files and fail. I use Ajax and Bootstrap to implement a frontend client to upload/download CSV files.
🌐
Stack Overflow
stackoverflow.com › questions › 72083250 › how-to-read-data-from-csv-file-in-spring-boot-or-java
How to read data from CSV file in Spring Boot or Java? - Stack Overflow
So whenever I am trying to read, always giving me an exception of ArrayIndexOutOfBound. Below is my REST controller for importing data from CSV: @RequestMapping(value = AkApiUrl.uploadcsv, method = { RequestMethod.POST, RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<?> uploadcsv(HttpServletRequest request, HttpSession session, @RequestParam("file") MultipartFile file) { CustomResponse = ResponseFactory.getResponse(request); try { User userdata = null; User usersession = (User) session.getAttribute("user"); String line = ""; if (null != file) { if(ex
🌐
Baeldung
baeldung.com › home › persistence › spring persistence › spring data › externalize setup data via csv in a spring application
Setup Data via CSV in a Spring Application | Baeldung
November 6, 2023 - Learn how to use CSV files to hold the Setup data for a Spring web application, and how to fully load and persist that data from disk.
Top answer
1 of 3
3

I don't think that splitting this work onto multiple threads is going to provide much improvement, and may in fact make the problem worse by consuming even more memory. The main problem is using too much heap memory, and the performance problem is likely to be due to excessive garbage collection when the remaining available heap is very small (but it's best to measure and profile to determine the exact cause of performance problems).

The memory consumption would be less from the replace and split operations, and more from the fact that the entire contents of the file need to be read into memory in this approach. Each line may not consume much memory, but multiplied by millions of lines, it all adds up.

If you have enough memory available on the machine to assign a heap size large enough to hold the entire contents, that will be the simplest solution, as it won't require changing the code.

Otherwise, the best way to deal with large amounts of data in a bounded amount of memory is to use a streaming approach. This means that each line of the file is processed and then passed directly to the output, without collecting all of the lines in memory in between. This will require changing the method signature to use a return type other than List. Assuming you are using Java 8 or later, the Stream API can be very helpful. You could rewrite the method like this:

public static Stream<List<String>> readCsv(InputStream inputStream) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    return reader.lines().map(line -> Arrays.asList(line.replace("\"", "").split(",")));
}

Note that this throws unchecked exceptions in case of an I/O error.

This will read and transform each line of input as needed by the caller of the method, and will allow previous lines to be garbage collected if they are no longer referenced. This then requires that the caller of this method also consume the data line by line, which can be tricky when generating JSON. The JakartaEE JsonGenerator API offers one possible approach. If you need help with this part of it, please open a new question including details of how you're currently generating JSON.

2 of 3
1

Instead of trying out a different approach, try to run with a profiler first and see where time is actually being spent. And use this information to change the approach.

Async-profiler is a very solid profiler (and free!) and will give you a very good impression of where time is being spent. And it will also show the time spend on garbage collection. So you can easily see the ratio of CPU utilization caused by garbage collection. It also has the ability to do allocation profiling to figure out which objects are being created (and where).

For a tutorial see the following link.

🌐
YouTube
youtube.com › watch
How to read and write from csv file using spring boot - YouTube
Create a spring boot project and try to read data from CSV file. after that do the filters based on the salary and also write filter data into another CSV fi...
Published   July 13, 2024
🌐
CData
cdata.com › kb › tech › csv-jdbc-spring-boot.rst
How to connect to CSV Data from Spring Boot
First, we mark the CSV data source as our primary data source. Then, we create a Data Source Bean. Create a DriverManagerDataSource.java file and create a Bean within it, as shown below. If @Bean gives an error, Spring Boot may not have loaded properly. To fix this, go to File -> Invalidate Caches and restart.
🌐
YouTube
youtube.com › bouali ali
CSV file upload using Spring Boot | persist the data to database | Step by Step tutorial - YouTube
Buy me a coffee: https://ko-fi.com/boualiali 🔥 Secure your spot now and embark on your journey to becoming a Spring Boot master!🔥https://aliboucoding.com/p...
Published   November 13, 2023
Views   7K