How to read csv file in servlet?

0

This is trying to read a csv file in a Servlet / java, the problem is when reading the file in particular and know your data.

This is the form where I sent the file to the Servlet.

 <form id="form" method="post" class="upload-box" action="../ValidationFile" 
            enctype='multipart/form-data'>
    <div class="box-body"><div class="form-group">
      <label for="exampleInputFile">Carge Archivo</label>
      <input type="file" accept="csv" name="file" id="inputfile"required>
 </div>

  <div class="box-footer">
<button type="submit"  name="submit" class="btn btn-primary">SUBIR</button>
</div>

The following is the Servlet Code:

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    BufferedReader br = null;
    String line = " ";
    String filename = null;
    Part filepart = request.getPart("file");

    long size = filepart.getSize();
    String filenombre = getFileName(filepart);
    String contentType = filepart.getContentType();

    filename = getFileName(filepart);
    try (PrintWriter out = response.getWriter()) {
        out.println(filename);
        br = new BufferedReader(new FileReader(filename));
        while ((line = br.readLine()) != null) {
            String[] datos = line.split(";");
            //Imprime datos.
            out.println(datos[0]);
        }

    }

}

I already get the info from the file with Request.getPart (), I try as seen in the code, but the truth never enters the while and I think I'm not having a good time with the file for the FileReader class to read. )

    
asked by Danserver 30.10.2018 в 19:49
source

0 answers