首页 > 解决方案 > 从数据库下载文件:找不到类 java.io.ByteArrayInputStream 的序列化程序,也没有发现创建 BeanSerializer 的属性

问题描述

我正在尝试从数据库下载文件。我收到一个错误:

"没有为类 java.io.ByteArrayInputStream 找到序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链:org.springframework.http.ResponseEntity["body"]->org.springframework .core.io.ByteArrayResource["inputStream"]) "

我的代码如下所示:

    public ResponseEntity<Resource> 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 ResponseEntity.notFound().build();
            }
            //Set File Name
            String fileName = rs.get(0).getFileName();
            File theFile = new File(fileName);  
            FileOutputStream output = new FileOutputStream(System.getProperty("user.dir") + "\\src\\main\\resources\\excel\\"+theFile);
            //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];
            ByteArrayResource resource = new ByteArrayResource(buffer);
            String contentType = null;
            MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
            contentType = mimeTypesMap.getContentType(theFile);
            return ResponseEntity
                    .ok()
                    .contentType(MediaType.parseMediaType(contentType))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                    .body(resource)
                    ;
        } catch (SQLException e) {
            logger.error(e);
            e.printStackTrace();
        }

        return ResponseEntity.notFound().build();
    }
}

标签: java

解决方案


推荐阅读