Validation : how to check if the file being uploaded is in excel format? - Apache POI

こ雲淡風輕ζ 提交于 2019-12-07 11:58:27

You can't check that before feeding it to POI. Just catch the exception which POI can throw during parsing. If it throws an exception then you can just show a FacesMessage to the enduser that the uploaded file is not in the supported excel format.

Kos Petoussis

Please try to be more helpful to the poster. Of course you can test before poi. Regular tests, to be performed before the try/catch, include the following. I suggest a fail-fast approach.

  1. Is it a "good" file?

    1. if file.isDirectory() -> die and exit.
    2. if !file.isReadable() -> die and exit.
    3. if file.available <= 100 -> die and exit (includes file size zero)
    4. if file.size >= some ridiculous large number (check your biggest excel file and multiply by 10)
  2. File seems good, but is contents like Excel?

    1. Does it start with "ÐÏà" -> if not, die.
    2. Does it contain the text "Sheet"-> if not, die
    3. Some other internal excel bytes that I expected from you guys here.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!