简体   繁体   中英

Read excel file from URL using POI

I want to read an excel file from a URL using Apache POI in java. I used the code like this :

String urlStr = "<url>";
URL url = new URL(urlStr);
URLConnection uc = url.openConnection();
HSSFWorkbook wb = new HSSFWorkbook(uc.getInputStream())

This is giving me an exception :: java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes java.io.IOException: Unable to read entire header; 0 bytes read; expected 512 bytes Any idea why this is happening? Am I doing something wrong in converting the url to stream?

I had downloaded the excel to the local disk and created the POI object using FileInputStream and it works fine. Hence I think there is no problem with the excel. It should be some problem with the code reading the URL stream.

You should move to a newer version of POI. Older versions of POI were more sensitive to buffering on the input stream than newer ones were. I believe that with 3.7 it ought to be fully fixed.

If you can, move to POI 3.8 beta 2, or 3.7 Final. You may find 3.2 an improvement over 2.5, but they're both quite old releases so I can't be sure.

If you must stick with a very old version, you'll need to do some buffering yourself. Make sure at least the first 4 blocks (2048 bytes) are available before calling POI.

Try Googling Unable to read entire header; 0 bytes read; expected 512 bytes

Lots of hits mention POI and one in particular mentioned something about reading older Excel (95) files. Tried opening a different file through an url?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM