JellyMei 2015-02-15 07:38 采纳率: 50%
浏览 2054

poi3.11 linux 下解析了不excel2007 但win7下没问题

使用spring mvc 上传文件。POI 在linux 64位下解析不了excel2007文件。而win7 32位下没问题。使用wps创建的excel都没问题。经过调试发现window下,上传文件的时候文件头是847d1caa,当写入本地再读取后会将文件头改为504b0304(zip格式),所以可以正常运行。但linux下再读取文件头也不会变,导致POI识别不了。代码如下:
@RequestMapping(value = "uploadPriceXlsFile", method = RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest req, HttpServletResponse resp
) throws Exception
{
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
HashMap request = new HashMap();
if (isMultipart) {
DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024 * 20, null);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(1024 * 1024 * 20);

        List<FileItem> fileItems = upload.parseRequest(req);
        Iterator<FileItem> iter = fileItems.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                String name = item.getFieldName();
                String value = item.getString("UTF-8");
                request.put(name,value);
            } else {
            String filename = item.getName();  
               byte b[]=item.get();
            for(int i=0;i<5;i++){
            logger.debug(b[i]+"------------------------------");
            }
            //文件头显示为847d1caa  byte[-124,125,28,-86,123]
            logger.debug(Test.bytesToHexString(b)); 
            String realPath = req.getSession().getServletContext().getRealPath("/WEB-INF/upload");
            //File uploadFile = new File(realPath+"/"+filename);  
            FileUtils.copyInputStreamToFile(item.getInputStream(), new File(realPath,filename));
            File uploadFile = new File(realPath+"/"+filename); 
                InputStream in=new FileInputStream(uploadFile);
                in.read(b);
                logger.debug(b[0]+"---"+b[1]+"---"+b[2]);
                //window服务器则会变成504b0304 byte[80.75.3] <span></span>                                                     logger.debug(Test.bytesToHexString(b));
                logger.debug("编码格式"+"     "+System.getProperty("file.encoding"));
                return memberService.uploadPriceXLS(in,filename);
            }
        }

}
文件头847d1caa(对应byte[-124,125,28,-86,123..])是啥文件? 为啥win7读出来文件头就变504b0304(zip格式 对应byte[80.75.3....]) ,而linux则不会改变报以下错误(版本不支持) exception:org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]

请各位大侠帮帮忙,谢谢

  • 写回答

1条回答

  • oyljerry 2015-02-15 08:23
    关注

    像这种工作,office自然对windows支持好,你应该在windows下处理excel,linux下各种兼容问题肯定很多

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿