I'm having this bug when trying to read a csv file from an http request.
My CSV file looks like this:
idUser,status
123,block
456,unblock
789,block
And I'm receiving this:
idUser,status123,block456,unblock789,block
Somehow when I send through http request it simply disappear with the char of the end of line ('\n'). I've tried many Readers, such as Opencsv and javaCsv. And also received the file in many ways, like InputStream, String, file. Seems that the bug is in the requester, I've also tried different requesters, like Postman in Chrome, and Open HttpRequester in firefox. And also generate the file in different environments like windows and linux. I've tried to send in a windows to a linux and viceversa. The only ways it worked was converting to Base64 and parsing it to csv file inside the service, but I don't want my cliente to convert the file all the time. The other way was reading directly from the repository, but then won't be a WebService. I'm using JavaCsv to read the file right now https://www.csvreader.com/java_csv_samples.php.
My webService:
@PUT
@Path("/csvReader")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response editByCsvFile(InputStream csvFileInput){
try {
CsvReader csvFile = new CsvReader(new InputStreamReader(csvFileInput));
csvFile.readHeaders();
while (csvFile.readRecord()){
String idUser = csvFile.get("idUser");
String status = csvFile.get("status");
if(status.equals("block")){
blockUser(Long.parseLong(IdUser));
}
if(status.equals("unblock")){
unblockUser(Long.parseLong(IdUser));
}
}
csvFile.close();
} catch (UnsupportedEncodingException ex) {
log.info("Unsupported Encoding", ex);
} catch (IOException ex) {
log.info("IO Exeption", ex);
}
return Response.ok().build();
}
I've also tried to set the delimiter directly csvFile.setDelimiter(',');
and the end of line csvFile.setRecordDelimiter('\n');
with no success :/ Thank you in advance!