May be I am doing it worng by using MultipartFile upload feature.
I have to read data from csv file which will be chosen by the client through the browser. I used Mu
I used a buffer to read line by line and get from multipart the inputstream. Maybe is more code, but I find helpful read text file by lines.
BufferedReader br;
List<String> result = new ArrayList<>();
try {
String line;
InputStream is = multipart.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
System.err.println(e.getMessage());
}
The best solution that I found was
@PostMapping(value="/csv")
public ResponseEntity<Void> processUpload(@RequestParam MultipartFile file) {
BufferedReader fileReader = new BufferedReader(new
InputStreamReader(file.getInputStream(), "UTF-8"));
CSVParser csvParser = new CSVParser(fileReader, CSVFormat.DEFAULT);
Iterable<CSVRecord> csvRecords = csvParser.getRecords();
for (CSVRecord csvRecord : csvRecords) {
System.out.println(csvRecord);
}
...
This is an adaptative solution from https://bezkoder.com/spring-boot-upload-csv-file/
I figured out a workaround. I converted the file to bytes and then converted the bytes to String. From String I applied string.split() to get what I wanted out of the file.
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
String completeData = new String(bytes);
String[] rows = completeData.split("#");
String[] columns = rows[0].split(",");