How to return an image in Spring Boot controller and serve like a file system

前端 未结 4 1386
无人及你
无人及你 2020-12-08 16:28

I\'ve tried the various ways given in Stackoverflow, maybe I missed something.

I have an Android client (whose code I can\'t change) which is currently getting an im

相关标签:
4条回答
  • 2020-12-08 17:03

    Using Apache Commons, you can do this and expose the image on an endpoint

    @RequestMapping(value = "/image/{imageid}",method= RequestMethod.GET,produces = MediaType.IMAGE_JPEG_VALUE)
    public @ResponseBody byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {
        InputStream in = new ByteArrayInputStream(getImage(imageid));
        return IOUtils.toByteArray(in);
        }
    

    All images will be served at endpoint /image/{imageid}

    0 讨论(0)
  • 2020-12-08 17:15

    Finally fixed this... I had to add a ByteArrayHttpMessageConverter to my WebMvcConfigurerAdapter subclass:

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
        final List<MediaType> list = new ArrayList<>();
        list.add(MediaType.IMAGE_JPEG);
        list.add(MediaType.APPLICATION_OCTET_STREAM);
        arrayHttpMessageConverter.setSupportedMediaTypes(list);
        converters.add(arrayHttpMessageConverter);
    
        super.configureMessageConverters(converters);
    }
    
    0 讨论(0)
  • 2020-12-08 17:17

    I believe this should work:

    @RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET)
    public ResponseEntity<byte[]> getImage(@PathVariable("id") String id) {
        byte[] image = imageService.getImage(id);
        return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
    }
    

    Notice that the content-type is set for ResponseEntity, not for HttpServletResponse directly.

    0 讨论(0)
  • 2020-12-08 17:19

    In case you don't know the file/mime type you can do this.... I've done this where i take an uploaded file and replace the file name with a guid and no extension and browsers / smart phones are able to load the image no issues. the second is to serve a file to be downloaded.

    @RestController
    @RequestMapping("img")
    public class ImageController {
    
    @GetMapping("showme")
    public ResponseEntity<byte[]> getImage() throws IOException{
        File img = new File("src/main/resources/static/test.jpg");
        return ResponseEntity.ok().contentType(MediaType.valueOf(FileTypeMap.getDefaultFileTypeMap().getContentType(img))).body(Files.readAllBytes(img.toPath()));
    }
    @GetMapping("thing")
    public ResponseEntity<byte[]> what() throws IOException{
        File file = new File("src/main/resources/static/thing.pdf");
        return ResponseEntity.ok()
                .header("Content-Disposition", "attachment; filename=" +file.getName())
                .contentType(MediaType.valueOf(FileTypeMap.getDefaultFileTypeMap().getContentType(file)))
                .body(Files.readAllBytes(file.toPath()));
    }
    
    
    }   
    

    UPDATE in java 9+ you need to add compile 'com.sun.activation:javax.activation:1.2.0' to your dependencies this has also been moved or picked up by jakarta.see this post

    0 讨论(0)
提交回复
热议问题