1

Kindly Ignore the question: Stupid parsing error at my end.

I have an API which allows a file upload and reads it using Apache POI (or throw exception if the filetype is not .xlsx). However, it seems like WorkbookFactory.create(inputstream) is not generating the correct file. When I read the file in code I get null values when reading cells where data should be present. Code is as follows:

     @POST
        @Path("<path>")
        @Consumes(MediaType.MULTIPART_FORM_DATA)
        @Produces(MediaType.APPLICATION_JSON)
        public Response submitFile(@FormDataParam("file") InputStream uploadedInputStream,
                                         @FormDataParam("file") FormDataContentDisposition fileDetail verticalId) {
            try {
                StringBuilder fileNameBuilder = new StringBuilder();
                String[] names = fileDetail.getFileName().replace(" ", "_").split("\\.");

                Contents contents = fileParser.parse(uploadedInputStream);

            return Response.ok().build();
        }

public class XlsxParser implements FileParser {

    public Content parse(File file) throws IOException InvalidFileFormatException{
         Content content = new Content();

        Workbook file;
        try {
            designFile = WorkbookFactory.create(file);
        } catch (InvalidFormatException exception) {
            throw new InvalidFileFormatException("Invalid file format. Expected file type is .xlsx");
        }
        Sheet sheet = file.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.rowIterator();
        Row row = rowIterator.next();
        int cellCount = row.getPhysicalNumberOfCells();
        return content;
    }
} 

Same code works fine if I first create a .xlsx file from inputstream and then pass a File object to WorkbookFactory.create()

Anyone ever faced the same issue? Thanks for the help.

Naruto Uzumaki
  • 958
  • 4
  • 17
  • 37

1 Answers1

4

I faced a similar issue some time before. What I did was just rejected to use the Workbook.create(InputStream inp) method. As far as I know, this method is near to be deprecated. The official documentation tells that:

  1. Workbook.create(InputStream inp) consumes more memory that Workbook.create(File file). That's why it's much better to use the second method.

  2. Workbook.create(InputStream inp) needs inp to support marking and reseting or be wrapped into PushbackInputStream. It is not very tricky, but why to do this, if we can just create a new File from InputStream and use more memory-effective method?

  3. In order to properly release resources the Workbook should be closed after use. Using Workbook.create(File file) requires this too, so there is no profit to utilize Workbook.create(InputStream inp) here.

  4. All after all, authors of Apache POI advise to use Workbook.create(File file) method instead of Workbook.create(InputStream inp) where it's possible.

Community
  • 1
  • 1
arcquim
  • 1,060
  • 1
  • 14
  • 24