Spring Boot2.6にファイルをダウンロードする方法
環境
Windows10 64bit
Spring Boot 2.6.3
Spring Tool Suite 4
JavaSE 11
Thymeleaf 3
説明
public interface HttpServletResponse extends ServletResponse
ServletResponse インターフェースを拡張して、レスポンスを送信する際に HTTP 固有の機能を提供します。
使用例
1.ダウンロード画面(templates\download\index.html)
<!DOCTYPE html>
<html xmlns:th="https://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>ダウンロード画面</title>
</head>
<body>
<form method="post" th:action="@{/fileDownload}">
<input type="submit" value="ダウンロード" />
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="https://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>ダウンロード画面</title>
</head>
<body>
<form method="post" th:action="@{/fileDownload}">
<input type="submit" value="ダウンロード" />
</form>
</body>
</html>
<!DOCTYPE html> <html xmlns:th="https://thymeleaf.org"> <head> <meta charset="UTF-8"> <title>ダウンロード画面</title> </head> <body> <form method="post" th:action="@{/fileDownload}"> <input type="submit" value="ダウンロード" /> </form> </body> </html>
2.コントローラ側のクラス(FileDwController.java)
package com.example.demo;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class FileDwController {
// ダウンロードパス
private static final String DLPATH = "C:/study/springboot/download/";
// 画面の初期表示
@GetMapping("/download")
String index(Model model) {
return "/download/index";
}
// ファイルダウンロードで使用するHttpServletResponse
@PostMapping("/fileDownload")
public String download(HttpServletResponse response) {
try (OutputStream ost = response.getOutputStream();) {
// ダウンロードするファイル
Path filePath = Paths.get(DLPATH + "22.pdf");
// バイトに変換する
byte[] cft = Files.readAllBytes(filePath);
// レスポンスヘッダに値をセット
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=22.pdf");
response.setContentLength(cft.length);
ost.write(cft);
ost.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
package com.example.demo;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class FileDwController {
// ダウンロードパス
private static final String DLPATH = "C:/study/springboot/download/";
// 画面の初期表示
@GetMapping("/download")
String index(Model model) {
return "/download/index";
}
// ファイルダウンロードで使用するHttpServletResponse
@PostMapping("/fileDownload")
public String download(HttpServletResponse response) {
try (OutputStream ost = response.getOutputStream();) {
// ダウンロードするファイル
Path filePath = Paths.get(DLPATH + "22.pdf");
// バイトに変換する
byte[] cft = Files.readAllBytes(filePath);
// レスポンスヘッダに値をセット
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=22.pdf");
response.setContentLength(cft.length);
ost.write(cft);
ost.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
package com.example.demo; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @Controller public class FileDwController { // ダウンロードパス private static final String DLPATH = "C:/study/springboot/download/"; // 画面の初期表示 @GetMapping("/download") String index(Model model) { return "/download/index"; } // ファイルダウンロードで使用するHttpServletResponse @PostMapping("/fileDownload") public String download(HttpServletResponse response) { try (OutputStream ost = response.getOutputStream();) { // ダウンロードするファイル Path filePath = Paths.get(DLPATH + "22.pdf"); // バイトに変換する byte[] cft = Files.readAllBytes(filePath); // レスポンスヘッダに値をセット response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=22.pdf"); response.setContentLength(cft.length); ost.write(cft); ost.flush(); } catch (IOException e) { e.printStackTrace(); } return null; } }
3.動作確認
以下のURLでアクセスすると「download」フォルダ配下のindex.htmlが表示されます。
http://127.0.0.1:8080/download
「ダウンロード」ボタンを押すとダウンロードします。