Issue
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.
Solution
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();
}
}
Answered By - fiveelements