m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Spring BootでRestControllerで画像を出力する

簡単にresources配下にあるファイルを画面に表示する

Spring Bootの場合は@RestControllerを返す場合は指定のデータを返却するような感じになるので返り値をバイナリ形式で返してみる。

やること

  • 画像を読み込み
  • byte[]へ変換して設定
  • jpegデータとして返却

実装

pom.xml
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
Image01Controller.java
package com.example.controller;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.core.io.ResourceLoader;

@RestController
public class Image01Controller {
	
	@Autowired
	ResourceLoader resourceLoader;
	
	@ResponseBody
	@RequestMapping(value = "/getImage/{id}", method = {RequestMethod.GET })
	public HttpEntity<byte[]> getImage(@PathVariable String id) throws IOException {
		
		// リソースファイルを読み込み
		Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg");
		InputStream image = resource.getInputStream();
		
		// byteへ変換
		byte[] b = IOUtils.toByteArray(image);
		
		// レスポンスデータとして返却
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);		
		headers.setContentLength(b.length);
		return new HttpEntity<byte[]>(b, headers);
	}
	
}

わかったこと

  • RestControllerでバイナリを返すことができる
  • リソースファイルにアクセスするにはResourceLoaderを使用する
  • バイナリデータを返すことで画像などを表示することが可能

今回はここまで…

おまけ

ByteArrayOutputStreamでbyte[]変換した場合
package com.example.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.core.io.ResourceLoader;

@RestController
public class Image01Controller {
	
	@Autowired
	ResourceLoader resourceLoader;
	private int c;
	
	@ResponseBody
	@RequestMapping(value = "/getImage/{id}", method = {RequestMethod.GET })
	public HttpEntity<byte[]> getImage(@PathVariable String id) throws IOException {
		
		// リソースファイルを読み込み
		Resource resource = resourceLoader.getResource("classpath:" + "/static/image/" + id + ".jpg");
		InputStream image = resource.getInputStream();
		
		// byteへ変換
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		int c;
		while ((c = image.read()) != -1) {
			bout.write(c);
		}
		byte[] b = bout.toByteArray();
		
		// レスポンスデータとして返却
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		headers.setContentLength(b.length);
		return new HttpEntity<byte[]>(b, headers);
	}
	
}