java - Bug reading a Csv File from an http request (WebService) -
i'm having bug when trying read csv file http request.
my csv file looks this:
iduser,status 123,block 456,unblock 789,block
and i'm receiving this:
iduser,status123,block456,unblock789,block
somehow when send through http request disappear char of end of line ('\n'). i've tried many readers, such opencsv , javacsv. , received file in many ways, inputstream, string, file. seems bug in requester, i've tried different requesters, postman in chrome, , open httprequester in firefox. , generate file in different environments windows , linux. i've tried send in windows linux , viceversa. ways worked converting base64 , parsing csv file inside service, don't want cliente convert file time. other way reading directly repository, won't webservice. i'm using javacsv read file right 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 tried set delimiter directly csvfile.setdelimiter(',');
, end of line csvfile.setrecorddelimiter('\n');
no success :/ thank in advance!
you using multipart/form-data
. content type used when have form <input type="file">
to upload file need additional annotations. unfortunately seems there no standard way upload files jax-rs. depends on implementation, example can use jersey, cxf or resteasy. see responses in ticket jaxrs multipart
the code should looks this
@put @path("/csvreader") @consumes(mediatype.multipart_form_data) public response uploadfile( @defaultvalue("true") @formdataparam("enabled") boolean enabled, @formdataparam("file") inputstream uploadedinputstream, @formdataparam("file") formdatacontentdisposition filedetail) {
you have full example here https://stackoverflow.com/a/25889454/6371459.
Comments
Post a Comment