gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:48:45 28 4
gpt4 key购买 nike

我正在编写一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com