spring - GridFSDBFile 无法转换为 org.springframework.web.multipart.MultipartFile

标签 spring mongodb spring-mvc spring-data thymeleaf

我正在编写一个 spring mvc webapp,它使用类型为 MultipartFile 的图像,我将其转换为 byte[] 然后转换为 Inputstream 和使用 GridFsTemplate 将其存储在 MongoDB 中。

现在的问题是我想在网页中显示存储的图像,但每当我尝试这样做时,数据库都会将图像文件作为 GridFSDBFiles 返回,因此抛出以下异常:

java.lang.ClassCastException: com.mongodb.gridfs.GridFSDBFile cannot be cast to org.springframework.web.multipart.MultipartFile

这是我用于存储图像的 DAO:

public void saveScan(Scan scan) throws IOException {

    String owner = String.valueOf(scan.getPatientId());

    String fileName = String.valueOf(scan.getPatientId() + "" + scan.getScanType());

    Date date = new Date();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/YYYY HH:mm a");
    String uploadTime = simpleDateFormat.format(date);

    System.out.println("the scan type is " + scan.getScanType());

    DBObject metaData = new BasicDBObject();

    metaData.put("owner", owner);
    metaData.put("fileName", fileName);
    metaData.put("uploadTime", uploadTime);

    byte[] scanBytes = scan.getScan().getBytes();
    InputStream inputStream = new ByteArrayInputStream(scanBytes);
    scanDaoImpl.SaveScan(inputStream, fileName, "image/jpeg", metaData);
}

这是为了检索图像:

public MultipartFile findOneScan(BigInteger patientId) {

    MultipartFile multipartFile = (MultipartFile) gridFsTemplate
            .findOne(new Query(Criteria.where("metadata.owner").is(patientId)));
    return multipartFile;

这是我获取图像的 Controller

@ResponseBody
@RequestMapping(value = "/patients/{id}/scan", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> scanImage(@PathVariable("id") BigInteger id) throws IOException {

    logger.debug("scanImage() is finding Image to display");

    byte[] bs = patientScanServiceImpl.findOne(id).getBytes();

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.IMAGE_JPEG);
    httpHeaders.setCacheControl(CacheControl.noCache().getHeaderValue());

    return new ResponseEntity<byte[]>(bs, httpHeaders, HttpStatus.OK);
}

这是我的 thymeleaf 图片标签:

<span>
    <img th:src="@{/patients/{patientid}/scan(patientid=${patient.id})}" width="250" height="250"/>
</span>

最佳答案

现在我对此有了更多的了解,终于找到了解决办法。您不能直接将 gridFSDBFile 直接转换为 byte[ ];它必须先转换为 OutputStream,然后如果必须显示,则转换为 byte[ ]。所以我允许我的 DAO 方法返回一个 GridFSDBFile 但在服务层我将 GridFSDBFile 转换为 ByteArrayOutputStream 然后到 byte[ ]。 现在我的 DAO 检索图像的方法是

public GridFSDBFile findOneScan(BigInteger patientId, String scanType) {

    String fileName = String.valueOf(patientId + "" + scanType);

    GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(new Query(Criteria.where("metadata.fileName").is(fileName)));
    return gridFSDBFile;

我为 Controller 提供服务的服务层是

public byte[] findOne(BigInteger patientId, String scanType) throws IOException {

    GridFSDBFile gridFSDBFile = scanRepository.findOneScan(patientId, scanType);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gridFSDBFile.writeTo(outputStream);
    byte[] bs = outputStream.toByteArray();

    return bs;
}

而且整个过程都很好。

关于spring - GridFSDBFile 无法转换为 org.springframework.web.multipart.MultipartFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46113279/

相关文章:

Mongodb集合对象总小时数集合

MongoDB查询: $near with aggregation

c - 如何获取 MongoDB 服务器上的数据库和集合列表?

java - 我如何知道 Java 项目是基于 Spring 的还是基于 Spring MVC 的还是基于 Spring xxx 的?

java - Spring @Transactional 并发

java - 外部资源(JS)在 spring mvc app 中的位置

java - 如果用户未授权,Spring Security 显示 404

java - 使用 Spring 和 Jdbc 访问数据库

java - 405 获取资源时出错

java - Spring OXM 不适用于 Struts 1