我正在尝试使用Apache POI读取xlsx文件,但它给出了以下错误: org.apache.poi.EmptyFileException:提供的文件为空(零字节长)
我的控制器是:
@PostMapping("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public ResponseEntity<SalesforceLoadUser> uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
@NotNull @NotEmpty @RequestParam @DefaultValue(value = "false") final boolean testUser) throws IOException {
final SalesforceLoadUser sfData = salesforecEventService.uploadUserFile(uploadedInputStream, testUser);
final ResponseEntity<SalesforceLoadUser> finalResponse = new ResponseEntity<SalesforceLoadUser>(sfData, HttpStatus.OK);
log.debug("Data load started for test user",testUser, finalResponse.getStatusCode());
return finalResponse;
//return uploadSFUserFile(uploadedInputStream,testUser);
}
我的文件阅读器是:
@Override
public SalesforceLoadUser uploadUserFile(InputStream openInputStream , boolean testUsers) throws IOException {
final SalesforceLoadUser sfUserData = new SalesforceLoadUser();
try {
// Finds the workbook instance for XLSX file
Workbook myWorkBook = WorkbookFactory.create(openInputStream);
// Return first sheet from the XLSX workbook
Sheet mySheet = myWorkBook.getSheetAt(0);
// Get iterator to all the rows in current sheet
// Iterator<Row> rowIterator = mySheet.iterator();
// Traversing over each row of XLSX file
int rowsCount = mySheet.getLastRowNum();
System.out.println("Total Number of Rows: " + (rowsCount + 1));
for (int i = 1; i <= rowsCount; i++) {
Row row = mySheet.getRow(i);
int colCounts = row.getLastCellNum();
System.out.println("Total Number of Cols: " + colCounts);
for (int j = 0; j < colCounts; j++) {
//final Optional<CommsPhysician> checkUserRecord = repo.findPhysicianByNPI(row.getCell(0).getStringCellValue());
sfUserData.setNPI(row.getCell(0).getStringCellValue());
sfUserData.setFirstName(row.getCell(2).getStringCellValue());
sfUserData.setLastName(row.getCell(3).getStringCellValue());
sfUserData.setGender(row.getCell(14).getStringCellValue());
sfUserData.setGradYear((int) row.getCell(16).getNumericCellValue());
sfUserData.setClassification(row.getCell(4).getStringCellValue());
sfUserData.setEmail(row.getCell(15).getStringCellValue());
sfUserData.setStatus(row.getCell(1).getStringCellValue());
sfUserData.setSpecialty(row.getCell(17).getStringCellValue());
// Populate Roles
if (row.getCell(6).getBooleanCellValue()) {
sfUserData.setJvp(row.getCell(6).getBooleanCellValue());
}
if (row.getCell(5).getBooleanCellValue()) {
sfUserData.setMd(row.getCell(5).getBooleanCellValue());
}
if (row.getCell(7).getBooleanCellValue()) {
sfUserData.setCredentialed(row.getCell(7).getBooleanCellValue());
}
// Populate Address
sfUserData.setStreet(row.getCell(8).getStringCellValue());
sfUserData.setCity(row.getCell(9).getStringCellValue());
sfUserData.setState(row.getCell(10).getStringCellValue());
// Populate Palmer and Division
sfUserData.setPalmer(row.getCell(12).getStringCellValue());
sfUserData.setDivision(row.getCell(13).getStringCellValue());
// Set boolean for test user from request
sfUserData.setTestUser(testUsers);
salesforceBrokerService.publishToBroker(sfUserData);
}
}
//Close the workbook
myWorkBook.close();
} catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
e.printStackTrace();
} finally {
if (openInputStream != null){
//Close the excel input file (inputstream)
IOUtils.closeQuietly(openInputStream);
}
}
return sfUserData;
}
当Workbook尝试创建inputStream时抛出错误。
任何人都可以在这里提出错误的建议。我的xlsx文件既不是腐败也不是空的。