|
1 | 1 | package com.example.demo.login.controller;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.util.LinkedHashMap;
|
4 | 5 | import java.util.List;
|
5 | 6 | import java.util.Map;
|
6 | 7 |
|
7 | 8 | import org.springframework.beans.factory.annotation.Autowired;
|
| 9 | +import org.springframework.http.HttpHeaders; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.ResponseEntity; |
8 | 12 | import org.springframework.stereotype.Controller;
|
9 | 13 | import org.springframework.ui.Model;
|
10 | 14 | import org.springframework.web.bind.annotation.GetMapping;
|
@@ -153,8 +157,28 @@ public String postLogout() {
|
153 | 157 |
|
154 | 158 | // ユーザー一覧のCSV出力
|
155 | 159 | @GetMapping("/userList/csv")
|
156 |
| - public String getUserListCsv(Model model) { |
157 |
| - // TODO:あとで実装する |
158 |
| - return getUserList(model); |
| 160 | + public ResponseEntity<byte[]> getUserListCsv(Model model) { |
| 161 | + |
| 162 | + //ユーザーを全件取得して、CSVをサーバーに保存する |
| 163 | + userService.userCsvOut(); |
| 164 | + |
| 165 | + byte[] bytes = null; |
| 166 | + |
| 167 | + try { |
| 168 | + |
| 169 | + //サーバーに保存されているsample.csvファイルをbyteで取得する |
| 170 | + bytes = userService.getFile("sample.csv"); |
| 171 | + |
| 172 | + } catch (IOException e) { |
| 173 | + e.printStackTrace(); |
| 174 | + } |
| 175 | + |
| 176 | + //HTTPヘッダーの設定 |
| 177 | + HttpHeaders header = new HttpHeaders(); |
| 178 | + header.add("Content-Type", "text/csv; charset=UTF-8"); |
| 179 | + header.setContentDispositionFormData("filename", "sample.csv"); |
| 180 | + |
| 181 | + //sample.csvを戻す |
| 182 | + return new ResponseEntity<>(bytes, header, HttpStatus.OK); |
159 | 183 | }
|
160 | 184 | }
|
0 commit comments