5.20. ファイルダウンロード¶
目次
5.20.1. Overview¶
Note
コントローラクラスで、ファイルレンダリングのロジックを持たせることは推奨しない。
理由としては、コントローラの役割から逸脱するためである。 また、コントローラから分離することで、Viewの入れ替えが、容易にできる。
ファイルのダウンロード処理の概要を、以下に示す。
- DispatchServletは、コントローラへファイルダウンロードのリクエストを送信する。
- コントローラは、ファイル表示の情報を取得する。
- コントローラは、Viewを選択する。
- ファイルレンダリングは、Viewで行われる。
org.springframework.web.servlet.View インタフェースを提供している。org.springframework.web.servlet.view.document.AbstractPdfVieworg.springframework.web.servlet.view.document.AbstractXlsxVieworg.terasoluna.gfw.web.download.AbstractFileDownloadViewは、5.20.2. How to use¶
5.20.2.1. PDFファイルのダウンロード¶
org.springframework.web.servlet.view.document.AbstractPdfViewを継承したクラスを作成する必要がある。5.20.2.1.1. カスタムViewの実装¶
AbstractPdfViewを継承したクラスの実装例
@Component // (1)
public class SamplePdfView extends AbstractPdfView { // (2)
@Override
protected void buildPdfDocument(Map<String, Object> model,
Document document, PdfWriter writer, HttpServletRequest request,
HttpServletResponse response) throws Exception { // (3)
document.add(new Paragraph((Date) model.get("serverTime")).toString());
}
}
| 項番 | 説明 |
|---|---|
(1)
|
本例では、
@Componentアノテーションを使用して、component-scanの対象としている。後述する、
org.springframework.web.servlet.view.BeanNameViewResolverの対象とすることができる。 |
(2)
|
AbstractPdfViewを継承する。 |
(3)
|
buildPdfDocumentメソッドを実装する。 |
AbstractPdfViewは、PDFのレンダリングに、iTextを利用している。<dependencies>
<!-- omitted -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
<exclusion>
<artifactId>bctsp-jdk14</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>jfreechart</artifactId>
<groupId>jfree</groupId>
</exclusion>
<exclusion>
<artifactId>dom4j</artifactId>
<groupId>dom4j</groupId>
</exclusion>
<exclusion>
<groupId>org.swinglabs</groupId>
<artifactId>pdf-renderer</artifactId>
</exclusion>
<exclusion>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
<version>1.38</version>
</dependency>
</dependencies>
Note
itextのバージョンはSpring IO Platformにて定義されている。
5.20.2.1.2. ViewResolverの定義¶
org.springframework.web.servlet.view.BeanNameViewResolverとは、
Springのコンテキストで管理されたbean名を用いて実行するViewを選択するクラスである。
BeanNameViewResolverを使用する際は、通常使用する、
- JSP用の
ViewResolver(InternalResourceViewResolver) - Tiles用の
ViewResolver(TilesViewResolver)
より先にBeanNameViewResolverが実行されるように定義する事を推奨する。
Note
Spring FrameworkはさまざまなViewResolverを提供しており、複数のViewResolverをチェーンすることができる。
そのため、特定の状況では、意図しないViewが選択されてしまうことがある。
この動作は、ViewResolverに適切な優先順位を設定する事で防ぐことができる。
優先順位の設定方法は、ViewResolverの定義方法によって異なる。
- Spring Framework 4.1から追加された
<mvc:view-resolvers>要素を使用してViewResolverを定義する場合は、子要素に指定するViewResolverの定義順が優先順位となる。(上から順に実行される) - 従来通り
<bean>要素を使用してViewResolverを指定する場合は、orderプロパティに優先順位を設定する。(設定値が小さいものから実行される)
bean定義ファイル
<mvc:view-resolvers>
<mvc:bean-name /> <!-- (1) (2) -->
<mvc:jsp prefix="/WEB-INF/views/" />
</mvc:view-resolvers>
| 項番 | 説明 |
|---|---|
(1)
|
Spring Framework 4.1から追加された
<mvc:bean-name>要素を使用して、BeanNameViewResolverを定義する。 |
(2)
|
<mvc:bean-name>要素を先頭に定義し、通常使用するViewResolver(JSP用のViewResolver)より優先度を高くする。 |
Tip
<mvc:view-resolvers>要素はSpring Framework 4.1から追加されたXML要素である。
<mvc:view-resolvers>要素を使用すると、ViewResolverをシンプルに定義することが出来る。
従来通り<bean>要素を使用した場合の定義例を以下に示す。
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="0"/> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> <property name="order" value="1" /> </bean>
orderプロパティに、InternalResourceViewResolverより小さい値を指定し、優先度を高くする。
5.20.2.1.3. コントローラでのViewの指定¶
BeanNameViewResolverにより、コントローラで”samplePdfView”を返却することで、Javaソースコード
@RequestMapping(value = "home", params= "pdf", method = RequestMethod.GET)
public String homePdf(Model model) {
model.addAttribute("serverTime", new Date());
return "samplePdfView"; // (1)
}
| 項番 | 説明 |
|---|---|
(1)
|
“samplePdfView” をメソッドの戻り値として返却することで、
Springのコンテキストで管理された、
SamplePdfViewクラスが実行される。 |
5.20.2.2. Excelファイルのダウンロード¶
org.springframework.web.servlet.view.document.AbstractXlsxViewを継承したクラスを作成する必要がある。5.20.2.2.1. カスタムViewの実装¶
AbstractXlsxViewを継承したクラスの実装例
@Component // (1)
public class SampleExcelView extends AbstractXlsxView { // (2)
@Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception { // (3)
Sheet sheet;
Cell cell;
sheet = workbook.createSheet("Spring");
sheet.setDefaultColumnWidth(12);
// write a text at A1
cell = getCell(sheet, 0, 0);
setText(cell, "Spring-Excel test");
cell = getCell(sheet, 2, 0);
setText(cell, (Date) model.get("serverTime")).toString());
}
}
| 項番 | 説明 |
|---|---|
(1)
|
本例では、
@Componentアノテーションを使用して、component-scanの対象としている。前述した、
org.springframework.web.servlet.view.BeanNameViewResolverの対象とすることができる。 |
(2)
|
AbstractXlsxViewを継承する。 |
(3)
|
buildExcelDocumentメソッドを実装する。 |
AbstractXlsxViewは、EXCELのレンダリングに、Apache POIを利用している。<dependencies>
<!-- omitted -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<exclusions>
<exclusion>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependencies>
Note
poi-ooxmlが依存しているstax-apiはJava SEより標準で提供されるようになったため、不要なライブラリである。また、ライブラリの競合が発生する可能性があることから、
<exclusions>要素を追加し、当該ライブラリがアプリケーションに含まれないよう考慮する必要がある。
Note
poi-ooxmlのバージョンはSpring IO Platformにて定義されているものを利用するため、設定例では <version> を省略している。
また、
AbstractExcelViewはSpring Framework 4.2から@Deprecatedとなった。そのため、xlsファイルを使用したい場合も同様にAbstractXlsxViewの使用を推奨する。 詳細は、AbstractExcelViewのJavaDocを参照されたい。
5.20.2.2.2. ViewResolverの定義¶
設定は、PDFファイルをレンダリングする場合と同様である。詳しくは、ViewResolverの定義を参照されたい。
5.20.2.2.3. コントローラでのViewの指定¶
BeanNameViewResolverにより、コントローラで”sampleExcelView”を返却することで、Javaソース
@RequestMapping(value = "home", params= "excel", method = RequestMethod.GET)
public String homeExcel(Model model) {
model.addAttribute("serverTime", new Date());
return "sampleExcelView"; // (1)
}
| 項番 | 説明 |
|---|---|
(1)
|
“sampleExcelView” をメソッドの戻り値として返却することで、
Springのコンテキストで管理された、
SampleExcelViewクラスが実行される。 |
5.20.2.3. 任意のファイルのダウンロード¶
org.terasoluna.gfw.web.download.AbstractFileDownloadViewを継承したクラスを実装すればよい。AbstractFileDownloadViewでは、以下を実装する必要がある。- レスポンスボディへの書き込むためのInputStreamを取得する。
- HTTPヘッダに情報を設定する。
5.20.2.3.1. カスタムViewの実装¶
AbstractFileDownloadViewを継承したクラスの実装例
@Component // (1)
public class TextFileDownloadView extends AbstractFileDownloadView { // (2)
@Override
protected InputStream getInputStream(Map<String, Object> model,
HttpServletRequest request) throws IOException { // (3)
Resource resource = new ClassPathResource("abc.txt");
return resource.getInputStream();
}
@Override
protected void addResponseHeader(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) { // (4)
response.setHeader("Content-Disposition",
"attachment; filename=abc.txt");
response.setContentType("text/plain");
}
}
| 項番 | 説明 |
|---|---|
(1)
|
本例では、
@Componentアノテーションを使用して、component-scanの対象としている。前述した、
org.springframework.web.servlet.view.BeanNameViewResolverの対象とすることができる。 |
(2)
|
AbstractFileDownloadViewを継承する。 |
(3)
|
getInputStreamメソッドを実装する。ダウンロード対象の、
InputStreamを返却すること。 |
(4)
|
addResponseHeaderメソッドを実装する。ダウンロードするファイルに合わせた、 Content-Dispositionや、ContentTypeを設定する。
|
5.20.2.3.2. ViewResolverの定義¶
設定は、PDFファイルをレンダリングする場合と同様である。詳しくは、ViewResolverの定義を参照されたい。
5.20.2.3.3. コントローラでのViewの指定¶
BeanNameViewResolverにより、コントローラで”textFileDownloadView”を返却することで、Javaソース
@RequestMapping(value = "download", method = RequestMethod.GET)
public String download() {
return "textFileDownloadView"; // (1)
}
| 項番 | 説明 |
|---|---|
(1)
|
“textFileDownloadView” をメソッドの戻り値として返却することで、
Springのコンテキストで管理された、
TextFileDownloadViewクラスが実行される。 |
Tip
前述してきたように、SpringはModelの情報をいろいろなViewにレンダリングすることができる。 Springでは、Jasper Reportsのようなレンダリングエンジンをサポートし、さまざまなViewを返却することも可能である。 詳細は、Spring の公式ドキュメントSpring referenceを参照されたい。

