基本的には、「Apache Struts ハンドブック SECTION08.05」そのままだが、Excelファイルをダウンロードするように変更。

ファイルダウンロードAction

 public class DownloadAction extends Action{
   public final ActionForward execute(
      final ActionMapping map, final ActionForm form,
      final HttpServletRequest req, final HttpServletResponse res)
      throws Exception {
     // HTTP RESPONSE をリセット ("text;html"を返すように設定されている ) 
     res.reset();
     
     // HTTP RESPONSE のヘッダを設定
     res.setContentType("application/vnd.ms-excel");
     res.setHeader("Content-Disposition", "attachment;filename=data.xls");
     
     // ファイルの内容を書き込む
     OutputStream os = res.getOutputStream();
     InputStream is = new FileInputStream("/export/home/appwork/data.xls");
     
     byte[] buf = new byte[bufSize];
     int len;
     while ((len = input.read(buf)) != -1) {
       output.write(buf, 0, len);
     }
     
     is.close();
     os.flush(); // flush() を忘れずに、close()してはいけない
     
     return null;
   }
 }

ブラウザ(IE)上に表示する場合

HTTP RESPONSE の HEADER を変更する

方式HEADER
ダウンロードContent-Disposition=attachment;filename=data.xls
アプリ起動1Content-Disposition=inline; filename=data.xls
アプリ起動2Content-Disposition=file; filename=data.xls

Java#Struts


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2007-06-09 (土) 21:13:46 (6158d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=fe569b7c62
ISBN10
ISBN13
9784061426061