1

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!

Allan
  • 11
  • 2

1 Answers1

0

You are using multipart/form-data. This content type is used when you have a form with <input type="file">

To upload a file you need some additional annotations. Unfortunately seems there is no standard way to upload files with JAX-RS. Depends on implementation, for example you can use Jersey, CXF or RestEasy. See responses in this ticket Jaxrs multipart

The code should looks like 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 a full example here https://stackoverflow.com/a/25889454/6371459.

Community
  • 1
  • 1
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • Yep, I was using @FormDataParam before, but got some issues and decided to only receive directly an InputStream just to get things working, and also a MediaType as `@Consumes({"text/csv"})` just to see if I'm reading correctly the file and then I got this bug of not reading the end of line '\n'. Do you think if I get this Multipart_form_data working this bug is fixed? – Allan Jul 04 '16 at 20:02
  • Are you using html forms? If not, simply use `application/octet-stream` to send data as binary. If you have to deal with forms then you need to handle properly the file uploads – pedrofb Jul 04 '16 at 20:11
  • Yes, my cliente will use this service through an html form. I'll make things work with multipart first, and then I update here confirming the result. Thanks! – Allan Jul 04 '16 at 20:24