Java org.apache.poi.EmptyFileException:提供的文件为空(零字节长)
我试图使用Apache POI读取xlsx文件,但它给我以下错误: org.apache.poi.EmptyFileException:提供的文件为空(零字节长) 我的控制器是:Java org.apache.poi.EmptyFileException:提供的文件为空(零字节长),java,excel,apache,rest,apache-poi,Java,Excel,Apache,Rest,Apache Poi,我试图使用Apache POI读取xlsx文件,但它给我以下错误: org.apache.poi.EmptyFileException:提供的文件为空(零字节长) 我的控制器是: @PostMapping("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public ResponseEntity<SalesforceLoadUser> uploadFile(@FormDataParam("file") Input
@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);
}
@PostMapping(“/upload”)
@使用(MediaType.MULTIPART\u FORM\u数据)
公共响应上传文件(@FormDataParam(“文件”)InputStream uploadedInputStream,
@NotNull@NotEmpty@RequestParam@DefaultValue(value=“false”)final boolean testUser)抛出IOException{
最终SalesforceLoadUser sfData=SalesForceEventService.uploadUserFile(uploadedInputStream,testUser);
最终响应最终响应=新响应(sfData,HttpStatus.OK);
debug(“为测试用户启动数据加载”,testUser,finalResponse.getStatusCode());
返回最终响应;
//返回UploadsUserFile(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;
}
@覆盖
public SalesforceLoadUser uploadUserFile(InputStream openInputStream,boolean testUsers)引发IOException{
最终SalesforceLoadUser sfUserData=新SalesforceLoadUser();
试一试{
//查找XLSX文件的工作簿实例
工作簿My工作簿=WorkbookFactory.create(openInputStream);
//返回XLSX工作簿中的第一张工作表
工作表mySheet=myWorkBook.getSheetAt(0);
//获取当前工作表中所有行的迭代器
//迭代器rowIterator=mySheet.Iterator();
//遍历XLSX文件的每一行
int rowsunt=mySheet.getLastRowNum();
System.out.println(“总行数:”+(rowsCount+1));
对于(int i=1;i)尽管我可以想象,从阻塞流中读取时会遇到问题,但我会首先尝试(作为测试)将该InputStream存储到本地文件中。如果这看起来不错,您可能需要使用ByteArrayOutput/InputStream缓冲该InputStream,然后进行处理。