質問編集履歴

2

a

2016/07/17 00:37

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -222,7 +222,7 @@
222
222
 
223
223
 
224
224
 
225
- CsvWriter writer = new CsvWriter(new PrintWriter(new File("test.csv")));
225
+ CsvWriter writer = new CsvWriter(new PrintWriter(file));
226
226
 
227
227
  CsvEntityListHandler<TestBean> handler = new CsvEntityListHandler<TestBean>(
228
228
 

1

a

2016/07/17 00:37

投稿

k499778
k499778

スコア599

test CHANGED
File without changes
test CHANGED
@@ -147,3 +147,105 @@
147
147
 
148
148
 
149
149
  ```
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+ ---
158
+
159
+
160
+
161
+ [DLできるようになったコード]
162
+
163
+ ```java
164
+
165
+ package jp.co.test;
166
+
167
+
168
+
169
+ import java.io.File;
170
+
171
+ import java.io.IOException;
172
+
173
+ import java.io.PrintWriter;
174
+
175
+ import java.net.URLEncoder;
176
+
177
+ import java.nio.charset.StandardCharsets;
178
+
179
+ import java.nio.file.Files;
180
+
181
+ import java.util.ArrayList;
182
+
183
+ import java.util.List;
184
+
185
+
186
+
187
+ import javax.servlet.http.HttpServletResponse;
188
+
189
+
190
+
191
+ import org.springframework.stereotype.Controller;
192
+
193
+ import org.springframework.web.bind.annotation.RequestMapping;
194
+
195
+
196
+
197
+ import com.orangesignal.csv.CsvWriter;
198
+
199
+ import com.orangesignal.csv.handlers.CsvEntityListHandler;
200
+
201
+
202
+
203
+ @Controller
204
+
205
+ public class OrangeSignalCsv {
206
+
207
+
208
+
209
+ @RequestMapping("/orangeSignal/csvDown")
210
+
211
+ public void csvDown(HttpServletResponse response) throws IOException {
212
+
213
+
214
+
215
+ File file = new File("test.csv");
216
+
217
+ // System.out.println("CSV file: " + file.getAbsolutePath()); // DLしない場合の保存先を出力
218
+
219
+ response.addHeader("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(file.getName(), StandardCharsets.UTF_8.name()));
220
+
221
+ Files.copy(file.toPath(), response.getOutputStream());
222
+
223
+
224
+
225
+ CsvWriter writer = new CsvWriter(new PrintWriter(new File("test.csv")));
226
+
227
+ CsvEntityListHandler<TestBean> handler = new CsvEntityListHandler<TestBean>(
228
+
229
+ TestBean.class);
230
+
231
+ List<TestBean> list = new ArrayList<TestBean>();
232
+
233
+ list.add(new TestBean(1, "A"));
234
+
235
+ list.add(new TestBean(2, "B"));
236
+
237
+ list.add(new TestBean(3, "C"));
238
+
239
+ handler.save(list, writer);
240
+
241
+ writer.close();
242
+
243
+
244
+
245
+ }
246
+
247
+ }
248
+
249
+
250
+
251
+ ```