java解析Excel,The supplied file was empty

利用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;
}
请问各位大手,这个问题是怎么产生的,该怎么解决呢,谢谢了

1个回答

你确认你的文件是excel格式的么?不要以为excel能打开,并且拥有xls/xlsx后缀的文件都是excel格式的,一些程序生成的根本就是文本格式、html、csv等等,也用了xls后缀。
这些文件excel能打开,但是poi是不能解析的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!