이미지 스트리밍

송준희·2021년 7월 7일
0

	@ApiOperation("이미지 조회")
    @GetMapping("/{filename}", produces = [MediaType.IMAGE_JPEG_VALUE])
    @ResponseStatus(HttpStatus.OK)
    public byte[] viewImage(
        @PathVariable filename: String
    ) {
    	return service.viewImage(filename)
    }
    

    public byte[] viewImage(String filename) {
        try (
                FileInputStream inputStream = new FileInputStream(filename);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
        ) {
            byte[] buffer = new byte[8192];
            int length;
            while ((length = inputStream.read(buffer)) != -1)
                outputStream.write(buffer, 0, length);
            return outputStream.toByteArray();
        } catch (IOException e) {
            throw new CannotViewImageException();
        }
    }
profile
오늘 달리면 내일 걸을 수 있다!

0개의 댓글