チュートリアル8(Dawnloadable)

スケルトンソース

最後に画像表示部分を実装します。スケルトン生成時に指定した photo という空ファイルが元になった Photo クラスを使用します。
HTML ではないものをブラウザへ送信する役目を持ちます。実際のソースを確認してみます。

tutorial3/logic/Photo.java①
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
package tutorial3.logic;
 
import tutorial3.*;
import tutorial3.base.*;
import paraselene.*;
import paraselene.supervisor.*;
import java.io.*;
 
/**
* photo
*/
public class Photo extends tutorial3.view.Photo implements Downloadable {
    /**
     * コンストラクタ。
     */
    public Photo() {
        super();
        init();
    }
 
    /**
     * 初期化。
     */
    public void init(){
    }
 
    /**
     * 別名URI設定。nullを返すと別名は設定しません。
     * @return URI。必ず".na"で終えて下さい。
     */
    public String getAliasURI() {
        return null;
    }
 
    /**
     * 出力情報の設定を行う。
     * @param from 遷移元ページ。直接呼ばれている場合はnullです。
     * @param req リクエスト内容。
     * @return 出力ページ。
     * nullを返すとthisをリターンしたのと同じ扱いにされます。
     * @exception PageException 処理の継続が不可能(ブラウザには500を返す)。
     */
    public Page outputMain( Page from, RequestParameter req ) throws PageException {
        // 引数より出力対象を特定して下さい。
        return this;
    }
 
    /**
     * 出力コンテントタイプ。
     * @return コンテントタイプ。
     */
    public String getContentType() {
        // 適切なものを返して下さい。
        return "application/octet-stream";
    }
 
    /**
     * 出力内容の取得。
     * @return ストリーム。最終的にフレームワークによりcloseされます。
     * @exception PageException 処理の継続が不可能(ブラウザには500を返す)。
     */
    public InputStream getInputStream() throws PageException {
        // ダウンロード対象を返して下さい。
        try {
            return new FileInputStream( "download file" );
        }
        catch( Exception e ) {
            throw new PageException( e );
        }
    }
}

view.Photo は inputMain 以外にも isAllowHistoryAdd をオーバーライドし、履歴追加を禁止しています。

コーディング

まずは、正しいコンテントタイプを指定します。

tutorial3/logic/Photo.java②
1:
2:
3:
4:
5:
    public String getContentType() {
        // 適切なものを返して下さい。
        //return "application/octet-stream";
        return "image/jpeg";
    }

HTTP リクエスト処理の流れは以下のようになります。

  1. Web サーバーへ HTTP リクエスト発生。
  2. Gate クラスが URL を検証し、遷移元ページを特定します。
    Photo クラス以外の、別ページの inputMain が呼ばれ、その戻り値(遷移先)に Photo が指定されます。
  3. Gate クラスは Photo の outputMain を呼び出します。
  4. Gate クラスは Photo の getStream を呼び出し、その内容をブラウザへ返します。

このため、Photo はどの画像を表示すべきか?の情報をリクエストパラメータで貰う事にします。
流れとしては outputMain でパラメータを検証し、出力すべき画像ファイルパスをインスタンス変数に保持。
getStream が呼ばれたら、その画像ファイルをオープンし、入力ストリームをリターンする、という処理フローにします。
リクエストパラメータは以下の形式とします。
xxx?article=記事の配列番号&image=記事の画像の配列番号
最新記事の下部画像なら、
xxx?article=0&image=1
です。

変数を宣言します。

tutorial3/logic/Photo.java③
1:
2:
3:
4:
5:
6:
7:
public class Photo extends tutorial3.view.Photo implements Downloadable {
    // 記事の指定
    public static final String ARTICLE = "article";
    // 画像の指定
    public static final String IMAGE = "image";
    // 出力ファイルパス
    private String file;

リクエストパラメータを解析します。

tutorial3/logic/Photo.java④
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
    public Page outputMain( Page from, RequestParameter req ) throws PageException {
        // 引数より出力対象を特定して下さい。
        try {
            int article_no = Integer.parseInt( req.getItem( ARTICLE ).getValue( 0 ) );
            int image_no = Integer.parseInt( req.getItem( IMAGE ).getValue( 0 ) );
            file = Article.db.get( article_no ).image[image_no];
        }
        catch( Exception e ) {
            throw new PageException( e );
        }
        return this;
    }

ファイルをオープンして返します。

tutorial3/logic/Photo.java⑤
1:
2:
3:
4:
5:
6:
7:
8:
9:
    public InputStream getInputStream() throws PageException {
        // ダウンロード対象を返して下さい。
        try {
            return new  FileInputStream( file );
        }
        catch( Exception e ) {
            throw new PageException( e );
        }
    }

チュートリアル9(画像表示)

最終更新: 2009/10/16
特に明示されていない限り、本Wikiの内容は次のライセンスに従います:CC Attribution 3.0 Unported
文書の先頭へ
SourceForge.JP
2009 © Akira Terasaki.  
Driven by DokuWiki The Apache Software Foundation