从 Restful 后端 java 读取上传的 xlsx 文件
Read uploaded xlsx file from Restful backend java
我正在通过 ajax-post 调用从我的 UI 上传 excel 文件,并尝试从我支持的 Restful 服务读取该文件 java 代码,但我无法正确打印 excel 文件内容。
文件名和其他属性打印正确。
上传的文件:test.xlsx
如果我使用下面的代码会出错
[java] org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.ExceptionAdapter: : null
兴趣点版本: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=\"([^\"]+)\".*$", "");
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);`
then i get following error
[java] Caused by: java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
但是如果直接读取 excel 文件它工作正常
我发现了我们需要将 excel 流式传输为
的问题
File.class instead of 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();
}
我正在通过 ajax-post 调用从我的 UI 上传 excel 文件,并尝试从我支持的 Restful 服务读取该文件 java 代码,但我无法正确打印 excel 文件内容。 文件名和其他属性打印正确。
上传的文件:test.xlsx
如果我使用下面的代码会出错
[java] org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.ExceptionAdapter: : null
兴趣点版本: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=\"([^\"]+)\".*$", "");
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);` then i get following error
[java] Caused by: java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
但是如果直接读取 excel 文件它工作正常
我发现了我们需要将 excel 流式传输为
的问题File.class instead of 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();
}