【问题标题】:Read uploaded xlsx file from Restful backend java从 Restful 后端 java 读取上传的 xlsx 文件
【发布时间】:2017-08-18 20:25:00
【问题描述】:

我正在通过 ajax-post 调用从我的 UI 上传 excel 文件,并尝试从我支持的 Restful 服务 java 代码中读取该文件,但我无法正确打印 excel 文件内容。 文件名和其他属性打印正确。

上传的文件:test.xlsx

如果我使用下面的代码会出错

[java] org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.ExceptionAdapter:  : null

POI 版本:3.8

上传.java

public Response upload(final MultipartFormDataInput input)throws Exception{
    for (InputPart part : input.getParts()) { // you might get multiple files
        final String disposition = part.getHeaders().getFirst("Content-Disposition");
        final String fileName =  disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
        InputStream inputStream = part.getBody(FileInputStream.class, null);
        try {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();

            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();

                while (cellIterator.hasNext()) {

                    Cell currentCell = cellIterator.next();
                    if (currentCell.getCellType() == Cell.CELL_TYPE_STRING) {
                        System.out.print(currentCell.getStringCellValue() + "--");
                    } else if (currentCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        System.out.print(currentCell.getNumericCellValue() + "--");
                    } else if(currentCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        System.out.print(currentCell.getBooleanCellValue() + "--");
                    } else {
                        System.out.print("<BLANK>--");
                    }
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    input.close();
    return Response.ok("OK").build();
}

如果我使用

InputStream inputStream = part.getBody(InputStream.class, null);` 然后我得到以下错误

 [java] Caused by: java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

但是如果直接读取excel文件就可以了

【问题讨论】:

  • 哪里抛出了异常?您使用的是什么版本的 Poi?
  • @DavidConrad :我编辑了问题,请检查
  • 听起来输入流被压缩或base64编码或以其他方式需要进一步处理才能传递给Poi,但这只是一个猜测。我建议您将流的全部内容复制到日志、控制台或文件中,以便您检查它们。
  • 您可能需要检查this answer您的问题是否相同

标签: java ajax rest resteasy


【解决方案1】:

我发现了我们需要获取 excel 流的问题

File.class 而不是 InputStream.class

public Response upload(final MultipartFormDataInput input)throws Exception{
    Map<String, List<InputPart>> formDataMap = input.getFormDataMap();
    File inputStream = formDataMap.get("files").get(0).getBody(File.class, null);
    String extraField = formDataMap.get("extraField").get(0).getBodyAsString();
    String CustomField =  formDataMap.get("CustomField").get(0).getBodyAsString();

    try {

        Workbook workbook = WorkbookFactory.create(inputStream);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = datatypeSheet.iterator();

        while (iterator.hasNext()) {

            Row currentRow = iterator.next();
            Iterator<Cell> cellIterator = currentRow.iterator();

            while (cellIterator.hasNext()) {
                Cell currentCell = cellIterator.next();

                if (currentCell.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(currentCell.getStringCellValue() + "--");
                } else if (currentCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    System.out.print(currentCell.getNumericCellValue() + "--");
                } else if(currentCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    System.out.print(currentCell.getBooleanCellValue() + "--");
                } else {
                    System.out.print("<BLANK>--");
                }
            }
            System.out.println();

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    input.close();
    return Response.ok("OK").build();
}

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2022-01-15
    • 1970-01-01
    • 2021-03-20
    相关资源
    最近更新 更多