Как получить изображение из mongoDB и показать его на странице JSP с помощью весенней загрузки? - PullRequest
0 голосов
/ 14 июня 2019

я могу успешно разместить изображение в mongodb с помощью весенней загрузки.Теперь я хочу получить его и отобразить на странице JSP.Я много пробовал и не могу найти ответ. Может ли кто-нибудь иметь какой-либо пример.

Я пробовал некоторые коды из Интернета, но ни один из них не работал.

    @RequestMapping(value = "getImage", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
    public ResponseEntity<GridFsResource> getFile() {
        String fileId = "5cf6482fcf2e89340406b230";
        System.out.println(fileId);
        GridFSFile file = gridOperations.findOne(Query.query(Criteria.where("_id").is(fileId)));
        System.out.println(file + "******************To see in console******************");
        ResponseEntity<GridFsResource> k = ResponseEntity.ok().contentLength(file.getLength())
                .body(gridFsOperations.getResource(file));

        System.out.println(k + "******************To see in console******************");
        return k;
    }

******************** а также это ....................

@RequestMapping(value = "/image", method = RequestMethod.GET ,produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] download() throws IllegalStateException, IOException {    
        imageFileId =  "5cf6482fcf2e89340406b230";
        GridFSFile file = gridOperations.findOne(new 



Query(Criteria.where("_id").is(imageFileId)));    
System.out.println(file+"************* to confirm  *************")         

String gg = gridFsTemplate.getResource(file).toString();

        System.out.println(gg);

        InputStream inputStream = gridFsTemplate.getResource(file).getInputStream();            

        byte[] k = IOUtils.toByteArray(inputStream);
             return k;

    }
...