1. POI reads the exception and reports an error java.util.zip.zipexception: invalid stored block lengths
the system needs to export the excle receipt. The excle template is preset in the classpath (resources directory in idea). When the program is running, the class loader is used to read the file from the classpath as bufferedinput stream, and then the InputStream is used to instantiate the xssfworkbook object
The code is as follows:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(EXCEL_TEMPLATE);//Here the source files are obtained from the classpath, in the web environment all the source files are compressed under the war package
XSSFWorkbook workbook = new XSSFWorkbook(inputStream ); //error: java.util.zip.ZipException: invalid stored block lengths
About POI read excle operation, in the unit test is no problem! As for the difference between unit test and formal code, unit test is to obtain the file through absolute path, then convert the file into FileInputStream, and then instantiate xssfworkbook object with this input
The code is as follows:
File fileTemplate = new File("D:/workspace/template.xlsx");//As you can see here is the absolute path to get the source file
// Converting the stream
FileInputStream is = new FileInputStream(fileTemplate);
// Create a workbook
XSSFWorkbook workbook = new XSSFWorkbook(is);
// This code runs ok
Observing the above two pieces of code, we can find that the differences lie in two points: 1) different types of streams; 2) a source file is compressed, and a source file is not compressed
With regard to the difference of stream types, the parameter received by instantiating xssfworkbook object is an instance of the abstract class InputStream. The streams of the above two pieces of code inherit InputStream. According to the idea of polymorphism, the input parameters of the above two pieces of code are OK
Then the possible reason for the problem is that one source file is compressed, and the other is not compressed. After all, the error message is: java.util.zip.zipexception: invalid stored block lengths may be related to the compressed war package
So I unzipped the war package and tried to open the excle file under the classpath. I found that it could not be opened and the file was damaged
Therefore, it is basically determined that the reason why the file compression is not available when Maven install is packaged
So add plug-ins to Maven
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.6</version>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension> //xlsx结尾的文件不
</nonFilteredFileExtensions>
</configuration>
</plugin>
To prevent corrupting your binary files when filtering is enabled, you can configure a list of file extensions that will not be filtered.
https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
Similar Posts:
- [Solved] Java read excel file unable to recognize ole stream error
- Python FAQ – error using openpyxl module: zipfile.BadZipFile : File is not a zip file
- Spring Boot UrlResource loading classpath error: unknown protocol: classpath
- Unit test reports an error nosuchbeandefinitionexception
- The problem of JUnit test report class not found in maven
- java.net.SocketException: socket closed
- YUI Compressor Maven: Compressing and obfuscating JavaScript and CSS files
- [Solved] java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException (Upload Images Error)
- org.xml.sax.SAXParseException: Content is not allowed in trailing section
- Spring boot project integration redisson throw exception NoClassDefFoundError: lorg / nustaq / serialization / fstconfiguration