Requirement:
- maven
- jdk 8
- intelliJ
How To:
nambahin dependency ini untuk membaca file csv di pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.fluex404</groupId> <artifactId>springboot-upload-read-csv</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-upload-file-read-csv</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-csv</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
buat package api dan buat class CSVController.java
package com.fluex404.springbootuploadreadcsv.api; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.apache.coyote.Response; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping(value ="/api/csv") public class CSVController { @PostMapping("/upload") public ResponseEntity upload( @RequestParam("file")MultipartFile file) { if (isCSVFile(file)) { try { return ResponseEntity.ok(csvToListModelMap(file)); } catch (Exception e) { return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } else { return new ResponseEntity("file isn't csv", HttpStatus.NOT_ACCEPTABLE); } } private boolean isCSVFile(MultipartFile file){ if(!"text/csv".equals(file.getContentType())){ return false; } return true; } private List<ModelMap> csvToListModelMap(MultipartFile file) throws Exception { BufferedReader fileReader = new BufferedReader( new InputStreamReader(file.getInputStream(), "UTF-8")); CSVParser csvParser = CSVFormat.EXCEL.withFirstRecordAsHeader().withDelimiter(';').parse(fileReader); List<ModelMap> list = new ArrayList<>(); Iterable<CSVRecord> csvRecords = csvParser.getRecords(); for (CSVRecord csvRecord : csvRecords) { ModelMap m = new ModelMap(); m.put("no", csvRecord.get("User Number")); m.put("name", csvRecord.get("User Name")); m.put("email", csvRecord.get("User Email")); list.add(m); } return list; } }
lalu test pakai posman seperti ini:
github project: https://github.com/fluex404/springboot-upload-file-read-csv
Komentar
Posting Komentar