I have the following code:
@POST
@Path("/csv")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String populateCSV(@FormDataParam("data") InputStream fileInputStream) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
File initialFile = new File("/Users/me/Downloads/file.csv");
InputStream targetStream = FileUtils.openInputStream(initialFile);
CSVReader reader = new CSVReader(new InputStreamReader(targetStream), ',', '"', 0);
CSVReader jerseyReader = new CSVReader(new InputStreamReader(fileInputStream), ',', '"', 0);
List<String[]> fileAllRows = reader.readAll();
List<String[]> jerseyAllRows = jerseyReader.readAll();
return null;
}
jerseyAllRows
which is created out of CSVReader
that reads Jersey conversion of File to InputStream returns empty lines, while fileAllRows
which is created out of a FileInputStream
that contains the same file that is being submitted to jersey, returns 3 lines.
What makes the way that Jersey2 reads file create a different InputStream?
I need to post a file to Jersey2 and be able to parse it with OpenCSV
EDITED
If I convert the jersey input stream into String like this:
InputStream is = new ByteArrayInputStream(IOUtils.toString(inputStream).getBytes());
reader = new CSVReader(new InputStreamReader(is), ',', '"', 0);
I do get lines. but that's a waste of memory :( Any idea?
First, you should try uniVocity-parsers CSV parser as it is twice as fast and has many additional features.
Sencond, you should provide the encoding of your input.
Third, you may also need to specify the line separators explicitly
Example using uniVocity-parsers :
File initialFile = new File("/Users/me/Downloads/file.csv");
InputStream targetStream = FileUtils.openInputStream(initialFile);
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new InputStreamReader(targetStream, "UTF-8"));
Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.