利用poi包解析excel文件的时候,出现以下错误
org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74)
at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:175)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209)
at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:98)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:295)
at com.zzidc.tax.bookkeeping.util.XlsUtils.readExcel(XlsUtils.java:53)
at com.zzidc.tax.bookkeeping.service.GenerateReportService.getRoleForExcel(GenerateReportService.java:254)
at com.zzidc.tax.bookkeeping.service.GenerateReportService.analysisImage(GenerateReportService.java:110)
at com.zzidc.tax.rabbitmq.CustomerChoose.analy(CustomerChoose.java:188)
at com.zzidc.tax.rabbitmq.CustomerChoose.processAndSaveDeclareImg(CustomerChoose.java:169)
at com.zzidc.tax.rabbitmq.CustomerChoose.access$2(CustomerChoose.java:155)
at com.zzidc.tax.rabbitmq.CustomerChoose$1.handleDelivery(CustomerChoose.java:80)
at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:144)
at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:99)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
我的代码如下:
//读取excel
public static Workbook readExcel(String filePath){
Workbook wb = null;
InputStream is = null;
URLConnection conn = null;
if(filePath==null){
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
try {
URL url = new URL(filePath);
conn = url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3 * 60 * 1000);
is = conn.getInputStream();
wb = StreamingReader.builder()
.rowCacheSize(100)
.bufferSize(1024 * 1024 * 5)
.open(is);
if(".xls".equals(extString)){
wb = new HSSFWorkbook(is);
}else if(".xlsx".equals(extString)){
wb = new XSSFWorkbook(is);
}else{
wb = null;
}
} catch (Exception e) {
logger.error("Excel工具读取文件发生异常,异常: " + e);
logger.error("文件: " + filePath);
e.printStackTrace();
return null;
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
return wb;
}
请问各位大手,这个问题是怎么产生的,该怎么解决呢,谢谢了