Struts 2ダウンロードファイルの例

Struts 2ダウンロードファイルの例

ダウンロード–Struts2-Download-File-Example.zip

ユーザーがファイルをダウンロードできるようにするカスタム結果タイプの使用を示すStruts 2の例。

1. アクション

Actionクラスで、InputStreamデータ型とそのゲッターメソッドを宣言しました。

DownloadAction.java

package com.example.common.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{

    private InputStream fileInputStream;

    public InputStream getFileInputStream() {
        return fileInputStream;
    }

    public String execute() throws Exception {
        fileInputStream = new FileInputStream(new File("C:\\downloadfile.txt"));
        return SUCCESS;
    }
}

2. ページを見る

ファイルをダウンロードするためのダウンロードリンクがある通常のページ。

downloadPage.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>



Struts 2 download file example

Download file - fileABC.txt

3. struts.xml

ダウンロードファイルの詳細を一目瞭然に定義します。 <param name=”inputName”>値は、アクションからのInputStreamプロパティの名前です。

詳細な説明については、このStruts 2 Stream Result documentationをお読みください。

struts.xml








   
    pages/downloadPage.jsp
   

   
    
      application/octet-stream
      fileInputStream
      attachment;filename="fileABC.txt"
      1024
    
   


4. それを実行します

Struts2 download file example