问题描述
- 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]请各位大侠帮帮忙,谢谢
解决方案
像这种工作,office自然对windows支持好,你应该在windows下处理excel,linux下各种兼容问题肯定很多
时间: 2024-10-29 04:45:22