2017-09-08 7 views
2

Je codage d'un ressort mvc webapp qui utilise des images de type MultipartFile que je convertir en byte[] puis à Inputstream et le stocker dans MongoDB utilisant GridFsTemplate. Maintenant, le problème est que je veux afficher les images stockées dans une page Web, mais chaque fois que je tente de la base de données renvoie le fichier d'image GridFSDBFiles et lance donc l'exception suivante:GridFSDBFile ne peut pas être jeté à org.springframework.web.multipart.MultipartFile

java.lang.ClassCastException: com. mongodb.gridfs.GridFSDBFile ne peut pas être jeté à org.springframework.web.multipart.MultipartFile

Ceci est mon DAO pour stocker des images:

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); 
} 

Et ceci est pour reteaving les images:

public MultipartFile findOneScan(BigInteger patientId) { 

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

Et ceci est mon contrôleur pour obtenir des images

@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); 
} 

Ceci est mon tag image thymeleaf:

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

Répondre

0

Maintenant, je suis plus instruit dans ce domaine, et je l'ai enfin trouvé un Solution. Vous ne pouvez pas lancer directement un gridFSDBFile directement à octet []; il doit d'abord être converti en OutputStream puis octet [] s'il doit être affiché. Alors je laissais ma méthode DAO pour retourner un GridFSDBFile mais dans la couche de service que je converti le GridFSDBFile à ByteArrayOutputStream puis à byte []. Maintenant, ma méthode DAO pour récupérer des images est

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; 

Et ma couche de service qui alimente le contrôleur est

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; 
} 

Et tout cela fonctionne très bien.