首页 > 解决方案 > XLSX 文件损坏:JDBC 文件上传和下载

问题描述

上传和下载正常,但我收到错误“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。”

文件也成功打开。我猜文件上传有问题。甲骨文数据库

这是上传部分

mapSqlParameterSource.addValue("inputFile", new SqlLobValue(uploadfiles, (int) fileMetaData.getSize()),
                OracleTypes.BLOB);

这里是下载部分

public Response downloadFile(String reorgId) throws IOException {
        List<WorkFlowInput> rs;
        try {
            //Fetching File from Database
            MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
            mapSqlParameterSource.addValue("reorgId", reorgId);
            rs = namedParameterJdbcTemplate.query(downloadFile, mapSqlParameterSource,
                    new BeanPropertyRowMapper<WorkFlowInput>(WorkFlowInput.class));
            //If File is empty
            if (rs == null || rs.isEmpty()) {
                ResponseMsg responseMsg = new ResponseMsg("","No file found!!");    
                return Response.ok(responseMsg).build();
            }
            //Set File Name
            String fileName = rs.get(0).getFileName();  
            String path = System.getProperty("user.dir") + "\\src\\main\\resources\\excel\\"+fileName;
            FileOutputStream output = new FileOutputStream(path);
            //Blob File
            Blob inputFile = rs.get(0).getInputFile();
            logger.info("Input File: "+ inputFile);
            InputStream input = inputFile.getBinaryStream();
            logger.info("InputStream input: "+ input);
            int fileLength = input.available();
            logger.info("fileLength = " + fileLength);
            byte[] buffer = new byte[1024];
            while (input.read(buffer) > 0) {
                output.write(buffer);
            }
            output.close();
            File theFile = new File(path);
            logger.info("Saved to file " + theFile.getAbsolutePath());
            String contentType = null;
            MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
            contentType = mimeTypesMap.getContentType(theFile);     
            ResponseBuilder response = Response.ok((Object) theFile); 
            response.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");  
            return response.build();
        } catch (SQLException e) {
            logger.error(e);
            e.printStackTrace();
        }
        ResponseMsg responseMsg = new ResponseMsg("","No file found!!");    
        return Response.ok(responseMsg).build();
    }

标签: javaspringjerseyblob

解决方案


推荐阅读