Struts – Webサイトのサンプルからファイルをダウンロードする
ユーザーがStruts Webプロジェクトからファイルをダウンロードできるようにするには、通常の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.プロジェクトウェブパス
ファイルが ”
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ダウンロードファイルの例をダウンロードする - link://wp-content/uploads/2010/04/Struts-Download-File-Example.zip[Struts-Download-File-Example.zip] === 1.アクションクラス 通常のHTMLページの代わりにアプリケーションファイルを返し、ダウンロードするための "superfish.zip"ファイルを取得するアクションクラス。
package com.mkyong.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設定ファイル。
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.3//EN”
“http://jakarta.apache.org/struts/dtds/struts-config
1
3.dtd”>
<struts-config>
<action-mappings>
<action path="/DownloadPage" type="org.apache.struts.actions.ForwardAction" parameter="/pages/display.jsp"/>
<action path="/DownloadIt" type="com.mkyong.common.action.DownloadFileAction" > </action>
</action-mappings>
</struts-config>
=== 4.テストする ** http://localhost:8080/StrutsExample/DownloadPage.do ** image://wp-content/uploads/2010/04/struts-download-file-example1.jpg[struts-download-file-example1、title = "struts-download-file-example1"] link://tag/download-file/[ダウンロードファイル]link://tag/struts/[struts]