[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]POI读取上传的xls文件,报错Invalid header signature

上一篇:[备忘]Cannot open include file: 'snappy-c.h'
下一篇:[GDS]LastTicketDate

添加日期:2018/11/1 22:30:00 快速返回   返回列表 阅读2130次

xls文件,单独main方法POI直接读取没有问题。

springMvc上传后,报错:Invalid header signature; read 0x0000000000000000 expetecd..

代码是这么写的
文件上传后,是一个CommonsMultipartFile file;
.
            try {
                XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
                return workbook;
            } catch (Exception e) {
                HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
                return workbook;
            }
inputStream就是file.getInputStream;

搜了半天,莫名其妙。
debug,发现上传后,file.getBytes()的字节流是正确的。
但是HSSFWorkbook里:
----------------------
           channel = Channels.newChannel(stream);
           
           // Get the header
           ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE);
           IOUtils.readFully(channel, headerBuffer);
           
           // Have the header processed
           _header = new HeaderBlock(headerBuffer);
--------------------------
把stream封装为channel,然后读取后,字节流就不对了,莫名其妙。
尝试这样改:

ByteArrayInputStream inputStream = new ByteArrayInputStream(file.getBytes());
就行了,起码不报错了。

真是见了鬼了。
-------------------------------------------
还有maven打war包时,文件头可能被替换的,需要改maven配置。
网上看的,没有试。


<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>



还有一种:


<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <webResources>
            <resource>
                <!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
                <directory>src/main/webapp/WEB-INF</directory>
                <!-- 目标路径 -->
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
            </resource>
        </webResources>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
            <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>
原文:https://blog.csdn.net/Olive_ZT/article/details/80726013 



仅供参考。
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved