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 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败