Java uses POI to read execel files when unable to read entire header; 0 bytes Read; The solution of expected bytes

Source: Internet
Author: User

The company originally in the Import Execel file, is an import; that is, each import file should have corresponding Java code to achieve, so in the reading Execel file when there is no problem, these two days, ready to waste the previous practice, unified encapsulation, To be able to parse all the Execel files and return the results as long as the configuration file is available.

In the implementation process, just started with the demo, so the Execel file is a full path (similar to C://test/xxx.xls), parsing time need to open the same Execel file two times, but there will be no problem. Later migrated to the project, because it was imported directly through the Web page, can only get its inputstream stream, can not get its absolute path. Due to the need to use the InputStream two times, it appeared unable to read entire header; 0 bytes Read; Expected bytes such a problem.

Search on the internet for a long time, a lot of solutions are not, and then think of inputstrem flow can only use once, the second failure, only to cause the above problems. There are two ways to solve this, (refer to http://simpleframework.net/bbs/835/27640.html), and I choose to first save this inputstrem stream with a byte number, Use the Bytearrayinputstream package (refer to http://hiok.blog.sohu.com/65555266.html) as follows:


        In the beginning, the stream object is saved with a byte number
        byte[] buf = Org.apache.commons.io.IOUtils.toByteArray (execelis)//execelis to InputStream stream
         /////////////////////////
         ///InputStream in the place where you need to use the InputStream
        bytearrayinputstream Bytearrayinputstream = new Bytearrayinputstream (BUF);
        Workbook wb = new Hssfworkbook (bytearrayinputstream);//bytearrayinputstream inherited InputStream, so it's no problem
        /// Other places to use are the same principle
        //Master: If you only need to use a inputstream flow, you don't need to do this, just use the OK


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.