-1

I have designed a servlet to read CSV, and I am passing CSV from postman as a form-data, but I am not able to get any option to read that data.

I tried below code

String body = request.getReader().lines().collect(Collectors.joining());

and I am getting a huge string, and I am not getting how to parse this one.

----------------------------140780421327043896517058Content-Disposition: form-data; name="CSVfile"; filename="dummyDataSet.csv"Content-Type: text/csvID,Name ,Age,Address,State,Date,Salary1,Airi Satou,33,Tokyo,Tokyo,11/28/2008,"$162,700 "2,Angelica Ramos,47,London,London,10/9/2009,"$162,700 "3,Ashton Cox,66,San Francisco,San Francisco,1/12/2009,"$86,000 "4,Bradley Greer,41,London,London,10/13/2012,"$132,000 "5,Brenden Wagner,28,San Francisco,San Francisco,6/7/2011,"$206,850 "6,Brielle Williamson,61,New York,New York,12/2/2012,"$372,000 "7,Bruno Nash,38,London,London,5/3/2011,"$163,500 "8,Caesar Vance,21,New York,New York,12/12/2011,"$106,450 "9,Cara Stevens,46,New York,New York,12/6/2011,"$145,600 "10,Cedric Kelly,22,Edinburgh,Edinburgh,3/29/2012,"$433,060 "----------------------------140780421327043896517058--

can you please help me, how to read the CSV data.

sheetal. gami
  • 47
  • 1
  • 10
  • May be below link will help . http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm – parthivrshah Aug 02 '19 at 06:33
  • thanks @parthivrshah, but it is not thing that i want – sheetal. gami Aug 02 '19 at 06:56
  • I am using dopost Method, in that how i will get csv data, from postman i am passing as 'form-data' – sheetal. gami Aug 02 '19 at 07:24
  • Looks like you are sending the CSV content in form-data as text. Why not send the CSV as a file (multipart/form-data)? Then in the servlet, you can access it as request.getPart(). Which version of Servelt you are using? If it is older than 3.0 then consider using commons upload. – fiveelements Aug 02 '19 at 08:04
  • @fiveelements, I am sending CSV as file only, I just want to read the content in dopost request. – sheetal. gami Aug 02 '19 at 08:46
  • Ok. Use `request.getParts()` or `request.getPart(name)` to read the part content as String or `InputStream`. – fiveelements Aug 02 '19 at 08:48
  • gettign error java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided – sheetal. gami Aug 02 '19 at 08:59

1 Answers1

2

Use request.getParts() or request.getPart(name) to read multipart content and add @MultipartConfig annotation to the servlet.

Here you go:

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

@WebServlet(urlPatterns = "/upload")
@MultipartConfig
public class MyFileUploadServelt extends HttpServlet {
    private static final String CONTENT_DISPOSITION_KEY = "content-disposition";
    private static final String FILE_NAME_KEY = "filename";
    private static final int BUFFER_SIZE = 2048;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Content Type: " + req.getContentType());
        System.out.println("Content Length: " + req.getContentLength());
        System.out.println("Parts: " + req.getParts().toString());
        System.out.println("Part 1: " + req.getPart("myfile"));
        // Read parts. Also can be read by directly calling req.getPart(fileName)
        for (Part part : req.getParts()) {
            System.out.println("File Name: " + getFileName(part));
            System.out.println("File Content: " + getTextFromPart(part));
        }
    }

    private String getFileName(Part part) {
        for (String contentDisposition : part.getHeader(CONTENT_DISPOSITION_KEY).split(";")) {
            if (contentDisposition.trim().startsWith(FILE_NAME_KEY)) {
                return contentDisposition.substring(contentDisposition.indexOf('=') + 1).trim().replace("\"", "");
            }
        }
        return null;
    }


    private String getTextFromPart(Part part) throws IOException {
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));
        StringBuilder value = new StringBuilder();
        char[] buffer = new char[BUFFER_SIZE];
        for (int length = 0; (length = reader.read(buffer)) > 0; ) {
            value.append(buffer, 0, length);
        }
        return value.toString();
    }
}
fiveelements
  • 3,649
  • 1
  • 17
  • 16