OpenCSV can't read InputStream created by Jersey2

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;
}

jerseyAllRowswhich is created from CSVReader, which reads the Jersey transform from a file into InputStream, returns empty lines, and fileAllRowswhich is created from FileInputStream, which contains the same file that is sent to the T-shirt, returns 3 lines.

What does the file that Jersey2 reads create another InputStream?

I need to send a file to Jersey2 and parse it using OpenCSV

EDITED

If I convert the input jersey to String as follows:

InputStream is = new ByteArrayInputStream(IOUtils.toString(inputStream).getBytes());
reader = new CSVReader(new InputStreamReader(is), ',', '"', 0);

I get the lines. but this is a waste of memory :( Any idea?

+4
source share
1 answer

-, uniVocity-parsers CSV, .

Sencond, .

-,

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"));

: . ( Apache V2.0).

+5

Source: https://habr.com/ru/post/1570440/


All Articles