Struts - Webサイトのサンプルからファイルをダウンロードする

Struts – Webサイトの例からファイルをダウンロードする

ユーザーがStrutsWebプロジェクトからファイルをダウンロードできるようにするには、通常のHTMLページの代わりにアプリケーションファイルを返すように「HttpServletResponse」に通知する必要があります。

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=downloadfilename.csv");

サーバー側では、3つの異なる方法からダウンロードファイルを取得できます。

1. ファイルシステム

ファイルシステムから取得する

FileInputStream in =
  new FileInputStream(new File("C:\\filename.zip"));
2. プロジェクトWebパス

ファイルが「http://yourname.com/StrutsExample/upload/filename.zip」にあり、「StrutsExample」がプロジェクト名(serlvetコンテキスト)であると想定します。

//jndi:/yourname.com/StrutsExample/upload/filename.zip
URL url = getServlet().getServletContext().getResource("upload/filename.zip");
InputStream in = url.openStream();
3. バイト配列

データベースからファイルまたはblobデータを取得する場合、通常はバイト配列として返されます。

byte[] bytes = new byte[4096];
InputStream in = new ByteArrayInputStream(bytes);

このStrutsダウンロードファイルの例をダウンロードする–Struts-Download-File-Example.zip

1. アクションクラス

通常のHTMLページの代わりにアプリケーションファイルを返し、ユーザーがダウンロードする「superfish.zip」ファイルを取得するアクションクラス。

package com.example.common.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DownloadFileAction extends Action{

   @Override
   public ActionForward execute(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response)
     throws Exception {

     //return an application file instead of html page
     response.setContentType("application/octet-stream");
     response.setHeader("Content-Disposition","attachment;filename=superfish.zip");

     try
     {
        //Get it from file system
        FileInputStream in =
            new FileInputStream(new File("C:\\superfish.zip"));

        //Get it from web path
        //jndi:/localhost/StrutsExample/upload/superfish.zip
        //URL url = getServlet().getServletContext()
        //               .getResource("upload/superfish.zip");
        //InputStream in = url.openStream();

        //Get it from bytes array
        //byte[] bytes = new byte[4096];
        //InputStream in = new ByteArrayInputStream(bytes);

        ServletOutputStream out = response.getOutputStream();

        byte[] outputByte = new byte[4096];
        //copy binary content to output stream
        while(in.read(outputByte, 0, 4096) != -1){
            out.write(outputByte, 0, 4096);
        }
        in.close();
        out.flush();
        out.close();

     }catch(Exception e){
        e.printStackTrace();
   }

   return null;
  }
}

2. JSPページ

Download file from server -

3. struts-config.xml

Struts構成ファイル。