├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
├── org.eclipse.jdt.core.prefs
└── org.eclipse.m2e.core.prefs
├── pom.xml
├── springboot-csvfile-upload-mysql.postman_collection.json
├── src
└── main
│ ├── java
│ └── com
│ │ └── dot
│ │ └── s3
│ │ ├── SpringBootCSV.java
│ │ ├── Student.java
│ │ ├── controller
│ │ └── UploadController.java
│ │ ├── dao
│ │ └── StudentDAO.java
│ │ ├── repository
│ │ └── StudentRepository.java
│ │ └── service
│ │ └── UploadService.java
│ └── resources
│ └── application.yml
└── students.csv
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Spring-boot-csvfileupload
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding//src/main/java=UTF-8
3 | encoding//src/main/resources=UTF-8
4 | encoding/=UTF-8
5 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3 | org.eclipse.jdt.core.compiler.compliance=1.8
4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5 | org.eclipse.jdt.core.compiler.source=1.8
6 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 | com.360dot
7 | spring-boot-csv-file-upload
8 | jar
9 | 1.0
10 |
11 |
12 | org.springframework.boot
13 | spring-boot-starter-parent
14 | 1.4.3.RELEASE
15 |
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-devtools
30 | true
31 |
32 |
33 |
34 | com.opencsv
35 | opencsv
36 | 3.9
37 |
38 |
39 |
40 | com.h2database
41 | h2
42 | runtime
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-starter-data-jpa
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | org.springframework.boot
56 | spring-boot-maven-plugin
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/springboot-csvfile-upload-mysql.postman_collection.json:
--------------------------------------------------------------------------------
1 | {
2 | "id": "c24e8d09-7f84-5f86-7cd7-e28a5c77d43d",
3 | "name": "springboot-csvfile-upload-mysql",
4 | "description": "",
5 | "order": [
6 | "a39e9fc7-fc1c-a04b-b5e0-16bcc98e8fd0",
7 | "23406c32-ce40-6001-f8f8-f758122c2ed1"
8 | ],
9 | "folders": [],
10 | "folders_order": [],
11 | "timestamp": 1515471968750,
12 | "owner": "3426983",
13 | "public": false,
14 | "requests": [
15 | {
16 | "id": "23406c32-ce40-6001-f8f8-f758122c2ed1",
17 | "headers": "",
18 | "headerData": [],
19 | "url": "localhost:8080/students",
20 | "queryParams": [],
21 | "pathVariables": {},
22 | "pathVariableData": [],
23 | "preRequestScript": null,
24 | "method": "GET",
25 | "collectionId": "c24e8d09-7f84-5f86-7cd7-e28a5c77d43d",
26 | "data": null,
27 | "dataMode": "params",
28 | "name": "localhost:8080/students",
29 | "description": "",
30 | "descriptionFormat": "html",
31 | "time": 1515471987613,
32 | "version": 2,
33 | "responses": [],
34 | "tests": null,
35 | "currentHelper": "normal",
36 | "helperAttributes": {}
37 | },
38 | {
39 | "id": "a39e9fc7-fc1c-a04b-b5e0-16bcc98e8fd0",
40 | "headers": "",
41 | "headerData": [],
42 | "url": "localhost:8080/students/upload",
43 | "queryParams": [],
44 | "pathVariables": {},
45 | "pathVariableData": [],
46 | "preRequestScript": null,
47 | "method": "POST",
48 | "collectionId": "c24e8d09-7f84-5f86-7cd7-e28a5c77d43d",
49 | "data": [
50 | {
51 | "key": "file",
52 | "value": "",
53 | "description": "",
54 | "type": "file",
55 | "enabled": true
56 | }
57 | ],
58 | "dataMode": "params",
59 | "name": "localhost:8080/students/upload",
60 | "description": "",
61 | "descriptionFormat": "html",
62 | "time": 1515471976385,
63 | "version": 2,
64 | "responses": [],
65 | "tests": null,
66 | "currentHelper": "normal",
67 | "helperAttributes": {}
68 | }
69 | ]
70 | }
--------------------------------------------------------------------------------
/src/main/java/com/dot/s3/SpringBootCSV.java:
--------------------------------------------------------------------------------
1 | package com.dot.s3;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringBootCSV {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringBootCSV.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/dot/s3/Student.java:
--------------------------------------------------------------------------------
1 | package com.dot.s3;
2 |
3 | import java.util.Date;
4 |
5 | import javax.persistence.Entity;
6 | import javax.persistence.GeneratedValue;
7 | import javax.persistence.GenerationType;
8 | import javax.persistence.Id;
9 |
10 | import com.fasterxml.jackson.annotation.JsonInclude;
11 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
12 | import com.opencsv.bean.CsvBindByName;
13 |
14 | @Entity
15 | @JsonInclude(Include.NON_EMPTY)
16 | public class Student {
17 |
18 | @Id
19 | @GeneratedValue(strategy = GenerationType.AUTO)
20 | private int studentId;
21 |
22 | @CsvBindByName
23 | private String firstName;
24 |
25 | @CsvBindByName
26 | private String middleName;
27 |
28 | @CsvBindByName
29 | private String lastName;
30 |
31 | private String email;
32 |
33 | @CsvBindByName
34 | private String phoneNumber;
35 |
36 | @CsvBindByName
37 | private Character gender;
38 |
39 | @CsvBindByName
40 | private String information;
41 |
42 | @CsvBindByName
43 | private Date dob;
44 |
45 | public int getStudentId() {
46 | return studentId;
47 | }
48 |
49 | public void setStudentId(int studentId) {
50 | this.studentId = studentId;
51 | }
52 |
53 | public String getFirstName() {
54 | return firstName;
55 | }
56 |
57 | public void setFirstName(String firstName) {
58 | this.firstName = firstName;
59 | }
60 |
61 | public String getMiddleName() {
62 | return middleName;
63 | }
64 |
65 | public void setMiddleName(String middleName) {
66 | this.middleName = middleName;
67 | }
68 |
69 | public String getLastName() {
70 | return lastName;
71 | }
72 |
73 | public void setLastName(String lastName) {
74 | this.lastName = lastName;
75 | }
76 |
77 | public String getEmail() {
78 | return email;
79 | }
80 |
81 | public void setEmail(String email) {
82 | this.email = email;
83 | }
84 |
85 | public String getPhoneNumber() {
86 | return phoneNumber;
87 | }
88 |
89 | public void setPhoneNumber(String phoneNumber) {
90 | this.phoneNumber = phoneNumber;
91 | }
92 |
93 | public Character getGender() {
94 | return gender;
95 | }
96 |
97 | public void setGender(Character gender) {
98 | this.gender = gender;
99 | }
100 |
101 | public String getInformation() {
102 | return information;
103 | }
104 |
105 | public void setInformation(String information) {
106 | this.information = information;
107 | }
108 |
109 | public Date getDob() {
110 | return dob;
111 | }
112 |
113 | public void setDob(Date dob) {
114 | this.dob = dob;
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/src/main/java/com/dot/s3/controller/UploadController.java:
--------------------------------------------------------------------------------
1 | package com.dot.s3.controller;
2 |
3 | import java.io.IOException;
4 | import java.util.List;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RequestPart;
10 | import org.springframework.web.bind.annotation.RestController;
11 | import org.springframework.web.multipart.MultipartFile;
12 |
13 | import com.dot.s3.Student;
14 | import com.dot.s3.service.UploadService;
15 |
16 | /**
17 | *
18 | * File upload to S3 bucket
19 | *
20 | * Reference Blog:
21 | *
22 | * @author Kirankumar Garaddi
23 | *
24 | * @Created 04 January 2018 21:45:16 +0530
25 | *
26 | */
27 | @RestController
28 | @RequestMapping("/students")
29 | public class UploadController {
30 |
31 | @Autowired
32 | private UploadService uploadService;
33 |
34 | @Autowired
35 | UploadController(UploadService uploadService) {
36 | this.uploadService = uploadService;
37 | }
38 |
39 | @RequestMapping(value = "/upload", method = RequestMethod.POST)
40 | public List uploadFile(@RequestPart(value = "file") MultipartFile multiPartFile) throws IOException {
41 | return uploadService.uploadFile(multiPartFile);
42 | }
43 |
44 | @RequestMapping
45 | public List getStudents(){
46 | return uploadService.getStudents();
47 | }
48 | }
--------------------------------------------------------------------------------
/src/main/java/com/dot/s3/dao/StudentDAO.java:
--------------------------------------------------------------------------------
1 | package com.dot.s3.dao;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Repository;
7 |
8 | import com.dot.s3.Student;
9 | import com.dot.s3.repository.StudentRepository;
10 |
11 | @Repository
12 | public class StudentDAO {
13 |
14 | @Autowired
15 | private StudentRepository studentRepository;
16 |
17 | public void batchStore(List studentList) {
18 | studentRepository.save(studentList);
19 | }
20 |
21 | public List getStudents() {
22 | return studentRepository.findAll();
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/dot/s3/repository/StudentRepository.java:
--------------------------------------------------------------------------------
1 | package com.dot.s3.repository;
2 |
3 | import org.springframework.data.jpa.repository.JpaRepository;
4 | import org.springframework.stereotype.Repository;
5 |
6 | import com.dot.s3.Student;
7 |
8 | @Repository
9 | public interface StudentRepository extends JpaRepository {
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/com/dot/s3/service/UploadService.java:
--------------------------------------------------------------------------------
1 | package com.dot.s3.service;
2 |
3 | import java.io.File;
4 | import java.io.FileOutputStream;
5 | import java.io.FileReader;
6 | import java.io.IOException;
7 | import java.io.Reader;
8 | import java.util.ArrayList;
9 | import java.util.Iterator;
10 | import java.util.List;
11 |
12 | import org.springframework.beans.factory.annotation.Autowired;
13 | import org.springframework.stereotype.Service;
14 | import org.springframework.web.multipart.MultipartFile;
15 |
16 | import com.dot.s3.Student;
17 | import com.dot.s3.dao.StudentDAO;
18 | import com.opencsv.bean.CsvToBean;
19 | import com.opencsv.bean.CsvToBeanBuilder;
20 |
21 | @Service
22 | public class UploadService {
23 |
24 | @Autowired
25 | private StudentDAO studentDAO;
26 |
27 | public List uploadFile(MultipartFile multipartFile) throws IOException {
28 |
29 | File file = convertMultiPartToFile(multipartFile);
30 |
31 | List mandatoryMissedList = new ArrayList();
32 |
33 | try (Reader reader = new FileReader(file);) {
34 | @SuppressWarnings("unchecked")
35 | CsvToBean csvToBean = new CsvToBeanBuilder(reader).withType(Student.class)
36 | .withIgnoreLeadingWhiteSpace(true).build();
37 | List studentList = csvToBean.parse();
38 |
39 | Iterator studentListClone = studentList.iterator();
40 |
41 | while (studentListClone.hasNext()) {
42 |
43 | Student student = studentListClone.next();
44 |
45 | if (student.getPhoneNumber() == null || student.getPhoneNumber().isEmpty()
46 | || student.getFirstName() == null || student.getFirstName().isEmpty()) {
47 | mandatoryMissedList.add(student);
48 | studentListClone.remove();
49 | }
50 | }
51 |
52 | studentDAO.batchStore(studentList);
53 | }
54 | return mandatoryMissedList;
55 | }
56 |
57 | private File convertMultiPartToFile(MultipartFile file) throws IOException {
58 | File convFile = new File(file.getOriginalFilename());
59 | FileOutputStream fos = new FileOutputStream(convFile);
60 | fos.write(file.getBytes());
61 | fos.close();
62 | return convFile;
63 | }
64 |
65 | public List getStudents() {
66 | return studentDAO.getStudents();
67 | }
68 | }
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Garaddik/Springboot-CSVfileupload-mysql/41f41959352d9d020b93483c1d87100d2a220d85/src/main/resources/application.yml
--------------------------------------------------------------------------------
/students.csv:
--------------------------------------------------------------------------------
1 | firstName,middleName,lastName,email,phoneNumber,gender,information
2 | Kirankumar,F,Garaddi,kg@gmail.com,85538897424,M,some info
3 | Vinay ,J,Gadagi,vinay@gmail.com,12345678,M,some info
4 | Vikram,,Mahisi,vikram@gmail.com,996662233,M,some info
5 | mahesh,d,d,d,,M,some info
6 |
--------------------------------------------------------------------------------