teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

ソースコードへ追記しました。

2016/09/06 02:19

投稿

itmsd
itmsd

スコア16

title CHANGED
File without changes
body CHANGED
@@ -17,6 +17,15 @@
17
17
 
18
18
 
19
19
  ###該当のソースコード
20
+
21
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22
+ doAction(request, response);
23
+ }
24
+
25
+ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
26
+ doAction(request, response);
27
+ }
28
+
20
29
  private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21
30
  HttpSession session = request.getSession(true);
22
31
  response.setHeader("Cache-Control", "no-cache");

2

ソースコードを追加しました。

2016/09/06 02:19

投稿

itmsd
itmsd

スコア16

title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,43 @@
17
17
 
18
18
 
19
19
  ###該当のソースコード
20
+ private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21
+ HttpSession session = request.getSession(true);
22
+ response.setHeader("Cache-Control", "no-cache");
23
+ response.setHeader("Pragma","no-cache");
24
+ request.setCharacterEncoding("UTF-8");
20
25
 
26
+ /*途中省略*/
27
+
28
+ synchronized (session) {
29
+ sc_ = getServletConfig().getServletContext();
30
+ connPool_ = (ConnectionPool)sc_.getAttribute("connPool");
31
+ loginUser = (SysUserBeans)session.getAttribute("loginUser");
32
+
33
+ String sceneStr = request.getParameter("scene");
34
+ int scene = -1;
35
+ if (sceneStr != null) scene = Integer.parseInt(sceneStr);
36
+ switch(scene) {
37
+ case 1: // 一覧
38
+ response.setContentType(CONTENT_TYPE);
39
+ showRows(request, response, session);
40
+ break;
41
+
42
+ /*途中省略*/
43
+
44
+ case 22: // CSV作成
45
+ downloadCSV(request, response, session);
46
+ break;
47
+
48
+ default: // 検索条件
49
+ response.setContentType(CONTENT_TYPE);
50
+ sc_.getRequestDispatcher("/view/sales/salesAggregateFrame.jsp").forward(request, response);
51
+ }
52
+ rows.setReqPrintUrl(null);
53
+ }
54
+ }
55
+
56
+
21
57
  private void downloadCSV(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
22
58
  SalesAggregateBeans rows = (SalesAggregateBeans)session.getAttribute("salesAggregate");
23
59
  SysUserBeans loginUser = (SysUserBeans)session.getAttribute("loginUser");
@@ -43,6 +79,7 @@
43
79
  ps.write(rows.getSum(1) + ",");
44
80
  ps.write(rows.getSum(2) + ",");
45
81
  ps.write(rows.getBalance());
82
+ ps.flush();
46
83
  }
47
84
  ps.close();
48
85
  ps = null;

1

タイトルを具体的にし、とソースコードを簡潔にいたしました。

2016/09/06 01:04

投稿

itmsd
itmsd

スコア16

title CHANGED
@@ -1,1 +1,1 @@
1
- サーブレットから動的に作成したCSVをダウンロードさせたいと思います
1
+ サーブレットから動的に作成したCSVを遅延なく記録させたい。
body CHANGED
@@ -1,84 +1,63 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- サーブレットでCSVを作成させて、作成が完了した際はダウンロードボタンを
3
+ サーブレットでCSVを作成させて遅延なくハードディスクへ記録させたいと思います。
4
- JSPで表示させて、それをクリックすると”開く”の選択肢が表示されますので、
5
- ”開く”をクリックしてExcelで表示させることを希望しております。
6
4
 
7
5
  ###発生している問題・エラーメッセージ
8
6
 
9
- サーブレットを実行し、作成直後表示されたダウンロードボタンをリック
7
+ サーブレットを実行し、CSVを作成するのですが、実際ードディスに記録
10
- すると、開けなかったり、一つ前に実行した内容だったりするのですが、
11
- ダウンロードボタンを5秒程度待ってからクリックしてやると問題ありせん
8
+ されるまでに5秒程度の遅延が発生して
12
9
 
13
- ソフト上は作成実行が終了しているのですが、実際にハードディスクに記録される
14
- のに時差があるようです。
15
-
16
10
  上記と同じことを、jdk1.4とtomcat3の組み合わせで実行しているときは、問題
17
11
  なく運用できておりましたが、jdk8とtomcat8の環境に変えてから上記の問題が
18
12
  発生するようになりました。
19
13
 
14
+ ソースコード問題、tomcat等の設定でお気づきの点がございましたら、
20
- 対応策をご教示いただければ、幸甚にぞんじます。
15
+ ご教示いただければ、幸甚にぞんじます。
21
16
 
22
17
 
23
18
 
24
19
  ###該当のソースコード
25
20
 
21
+ private void downloadCSV(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
22
+ SalesAggregateBeans rows = (SalesAggregateBeans)session.getAttribute("salesAggregate");
23
+ SysUserBeans loginUser = (SysUserBeans)session.getAttribute("loginUser");
24
+
26
25
  String fileName;
27
26
  fileName = sc_.getRealPath("/data/210.csv");
28
- rows.setReqPrintUrl(response.encodeURL("/example/data/210.csv"));
27
+ rows.setReqPrintUrl(response.encodeURL("/example/data/.csv"));
29
28
 
30
29
  FileOutputStream fo = new FileOutputStream(fileName);
31
30
  OutputStreamWriter ps = new OutputStreamWriter(fo, "SJIS");
32
31
 
33
- String br = System.getProperty("line.separator");
32
+ int iMax = rows.getSize();
34
33
 
35
- int iMax = rows.getSize();
36
34
  for (int i = 0; i < iMax; i++) {
37
35
  rows.getRow(i);
38
36
  ps.write("\"" + rows.getCd() + "\",\"");
39
- if (i < (iMax-1)) {
40
- ps.write(rows.getName() + "\",\"");
37
+ ps.write(rows.getName() + "\",\"");
41
- ps.write(rows.getAddress() + "\",\"");
38
+ ps.write(rows.getAddress() + "\",\"");
42
- ps.write(rows.getReadings() + "\",\"");
39
+ ps.write(rows.getReadings() + "\",\"");
43
- ps.write(rows.getSalesmanName() + "\",");
40
+ ps.write(rows.getSalesmanName() + "\",");
44
- } else {
45
- ps.write("*合計*\",,,,");
46
- }
47
41
  ps.write(rows.getCarryOver() + ",");
48
42
  ps.write(rows.getSum(0) + ",");
49
43
  ps.write(rows.getSum(1) + ",");
50
44
  ps.write(rows.getSum(2) + ",");
51
- if (rows.getReqTaxCalcFlag()) {
52
- ps.write(rows.getBalance() + ",");
53
- if (rows.getTaxDiv() == 1) {
54
- ps.write(rows.getNetTaxable() + ",");
55
- } else {
56
- ps.write("0,");
57
- }
58
- ps.write(rows.getSum(3) + br);
59
- } else {
60
- ps.write(rows.getBalance() + br);
45
+ ps.write(rows.getBalance());
61
- }
62
46
  }
63
- ps.flush();
64
47
  ps.close();
65
48
  ps = null;
66
- sc_.getRequestDispatcher("/view/sales/salesDownload.jsp").forward(request, response);
67
49
 
50
+ fo.close();
51
+ fo = null;
52
+ sc_.getRequestDispatcher("/view/sales/salesAggregate.jsp").forward(request, response);
53
+ }
68
54
  ###試したこと
69
55
 
70
- 上記コードの、ps=null;とsc_.getRequestDispatcher("/view/sales/salesDownload.jsp").forward・
71
- 間に下記のコードを挿入してファイルがディスクに記録するを待たせてでいます。
56
+ CSV出力件数はわずか5行でも100行程度でも、5秒程度遅延が発生います。
72
57
 
73
- try{
74
- // これで5秒スリープします。
75
- Thread.sleep(5000);
76
- } catch(InterruptedException e) {
77
- System.out.println(e.getMessage());
78
- }
79
-
80
58
  ###補足情報(言語/FW/ツール等のバージョンなど)
81
59
 
82
60
  ・Windows10
83
61
  ・jdk8
84
- ・tomcat8
62
+ ・tomcat8
63
+ (サーブレットの設定は、インストール直後の状態です。)