我通过ajax-post调用从我的UI上传excel文件并尝试从我支持的Restful服务java代码中读取,但我无法正确打印excel文件内容。 文件名和其他属性正确打印。
上传的文件:test.xlsx
如果我使用以下代码
,则会出错[java] org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.ExceptionAdapter: : null
POI版本:3.8
upload.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文件就可以了。
答案 0 :(得分:0)
我发现我们需要将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();
}