続 動的イメージ表示

web.xmlに以下のエントリを追加

<context-param>
    <param-name>imageFolder</param-name>
    <param-value>/test</param-value>
    <description>for image file work folder</description>
</context-param>

imageTest.html

<html xmlns:m="http://www.seasar.org/maya">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Image test</title>
</head>
<body>
ImageTest<br/>
<img m:src="#{test.image}"/>
</body>
</html>

test

private ServletContext context;
public void setContext(ServletContext context) {
    this.context = context;
}
public String getImage() {
    final String folder = context.getInitParameter("imageFolder");
    final String path = context.getRealPath(folder);
    int data;
    try {
        // テスト用に画像ファイル読込(実際はDBよりBLOBで取得等)
        FileInputStream in = new FileInputStream("/tmp/test.jpeg");
        FileOutputStream out = new FileOutputStream(path + "/hoge.jpg");
        while ((data = in.read()) != -1) {
            out.write(data);
        }
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return "/s2jsf-example/test/hoge.jpg";
}

# とりあえず動作確認レベルです。
# 出力された画像ファイルを削除するタイミング等の考慮は必要