相关文章推荐
眼睛小的野马  ·  Common application ...·  1 月前    · 
唠叨的碗  ·  【 云原生 kubernetes 】- ...·  1 月前    · 
长情的风衣  ·  Singularity 使用 - ...·  3 月前    · 
果断的饼干  ·  C#: Simple HTTP ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When I try to read xls file using the code below, I always get error: org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x65572D2D2D2D2D2D, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document.

This is my code:

public Result<List<IDto>> ReadExcelClassInfo2003(File file,
            Timestamp createTime, Timestamp updateTime, BigDecimal createBy,
            BigDecimal updateBy) {
        Result<List<IDto>> resultData = new Result<List<IDto>>();
        Integer numInsertSuccess = 0;
        if (file == null) {
            resultData.setErrorCode(ErrorCode.ERROR_FORMAT);
            return resultData;
        try {
            InputStream is = new FileInputStream(file);
            POIFSFileSystem fs = new POIFSFileSystem(is);
            Integer classType = ClassTypeEnum.CLASSROOM.getValue();
            Integer maxCol = ExcelConstant.MAX_COLUMN_CLASSROOM_INFO;
            workbook = new HSSFWorkbook(fs);
            HSSFSheet sheetClassInfo = workbook
                    .getSheetAt(0);
            if (sheetClassInfo == null) {
                resultData.setErrorCode(ErrorCode.ERROR_FORMAT);
                return resultData;
            //Some code to get data from excel file here.
            is.close();
            workbook.close();
        } catch (FileNotFoundException e) {
            resultData.setErrorCode(ErrorCode.ERROR_FORMAT);
            return resultData;
        } catch (IOException e) {
            resultData.setErrorCode(ErrorCode.ERROR_FORMAT);
            return resultData;
        } catch (Exception e) {
            resultData.setErrorCode(ErrorCode.ERROR_FORMAT);
            return resultData;
        if (numInsertSuccess == 0) {
            resultData.setErrorCode(ErrorCode.CLASS_DATA_INVALID);
            return resultData;
        resultData.setErrorCode(ErrorCode.IMPORT_SUCCESS);
        resultData.setMessage(numInsertSuccess.toString());
        return resultData;

My controller code :

@POST
    @Path("class/import")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed(Role.TRAINING_ADMIN)
//  public Response importClass(@FormParam("file") File file) {
    public Response importClass(@Multipart("file") File file) {
        LOGGER.info("Received PUT import class: file=" + file.length());
        if (checkTokenAndRole(new int[] {1, 11}).getStatus() != Response.Status.OK.getStatusCode()) {           
            return LoginError(checkToken().getStatus());                
        } else {
            String token = request.getHeader(HttpHeaders.AUTHORIZATION);
            String fileExtension = request.getHeader("FileExtension");
            return ClassService.getInstance().importClass(file, fileExtension,
                    token);

And the method are call by controller :

public Response importClass(File file, String fileExtension, String token) {
        Result<List<IDto>> result = new Result<List<IDto>>();
        try {
            ErrorDTO errorDto = new ErrorDTO();
            String data = "";
            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            double megabytes = (kilobytes / 1024);
            if (megabytes > ExcelConstant.MAX_FILE_SIZE) {
                errorDto.setErrorCode(ErrorCode.ERROR_FORMAT);
                data = Utility.toJSONString(errorDto);
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                        .entity(data).build();
            Timestamp createTime = new Timestamp(System.currentTimeMillis());
            Timestamp updateTime = new Timestamp(System.currentTimeMillis());
            BigDecimal createBy = null;
            BigDecimal updateBy = null;
            Result<UserInfoDTO> userInfo = DaoManager.getUserInfoDao()
                    .getUserInfoByToken(token);
            if (userInfo.getData() != null) {
                createBy = userInfo.getData().getId();
                updateBy = userInfo.getData().getId();
            result = DaoManager.getClassDao().importClass(file, fileExtension,
                    createTime, updateTime, createBy, updateBy);
            int errorCode = result.getErrorCode();
            String message = result.getMessage();
            errorDto = new ErrorDTO();
            errorDto.setErrorCode(errorCode);
            errorDto.setMessage(message);
            data = Utility.toJSONString(errorDto);
            // release memory
            userInfo = null;
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity(data).build();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            result.setStatus(Constant.INTERNAL_SERVER_ERROR);
            result.setErrorCode(ErrorCode.IMPORT_ERROR);
            return super.responseData(result);
public Result<List<IDto>> importClass(File file, String fileExtension,
            Timestamp createTime, Timestamp updateTime, BigDecimal createBy,
            BigDecimal updateBy) throws IOException {
        return ExportExcelService.getInstance().ReadExcelClassInfo2003(file,
                fileExtension, createTime, updateTime, createBy, updateBy);

I debuged and found the process always check new POIFSFileSystem and throw exception with error above. I tested with all xls file I have and have same error. Any can help me resolve this problem, and what the header 0x65572D2D2D2D2D2D ?

Thanks.

Please show how File file gets instantiated. Where is the file resource stored? Is it possible that Maven is envolved and "filters" the *.xls file? See stackoverflow.com/questions/10024185/…. – Axel Richter May 5, 2020 at 5:40 All I can tell you is that something corrupts your *.xls files before InputStream is = new FileInputStream(file); POIFSFileSystem fs = new POIFSFileSystem(is); runs. As you dont want showing how File file gets instantiated and how the *.xls files are coming into their locations, no further help is possible. – Axel Richter May 5, 2020 at 7:22 Version 3.13 is rather old and far away from current version. Current version is apache poi 4.1.2. But I doubt this will be the reason. The reason is that your File file is corrupt. – Axel Richter May 5, 2020 at 9:52 I am not a Java EE expert but I've never seen @Multipart("file") File file in context of a method which consumes multipart/form-data. There is a specilal MultipartFile class which most times is used. So I would suggest at first checking whether this works correctly. Let your controller method store the file somewhere and then check whether this is a proper Excel file. Is Excel able opening that stored file without problems? – Axel Richter May 6, 2020 at 5:40

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.