Salut!
J'obtiens une exception IllegalStateException en appelant la méthode :
Est-ce que c'est déjà arriver à qqun ou il faut que je donne un peu + de détails?Code:
1
2 ServletOutputStream out = response.getOutputStream();
Version imprimable
Salut!
J'obtiens une exception IllegalStateException en appelant la méthode :
Est-ce que c'est déjà arriver à qqun ou il faut que je donne un peu + de détails?Code:
1
2 ServletOutputStream out = response.getOutputStream();
http://java.sun.com/products/servlet...OutputStream()
En regardant la javadoc pour cette méthode, on trouve ça :
Citation:
Throws:
IllegalStateException - if the getWriter method has been called on this response
java.io.IOException - if an input or output exception occurred
ouais c'est exact, je viens de m'en rendre compte. :lol:
Mais je ne comprends pas, je ne trouve pas dans ma servlet ce getWriter.
ça me saoul :x
Regarde les lignes où tu utilises la response.
L'une d'elles commit peut-etre la response ....
Et si tu ne trouve pas, alors montre nous ton code...
Bon voilà mon code , c'est un peu long...:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public class DownloadServlet extends HttpServlet { protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("DOget Download"); doPost(request, response); } public void setFile( ServletOutputStream out, HttpServletResponse response, String fileName, String path, String lstrMimeType) throws ServletException { try { FileInputStream in = null; String lstrDownloadPath = ListLibrary.getDataPath(); FileOutputStream tempfile = new FileOutputStream(lstrDownloadPath + "/" + fileName); response.setContentType("multipart/download"); response.setHeader( "Content-Disposition", "attachment;filename=" + fileName); in = new FileInputStream(path); int availableLength = in.available(); byte[] totalBytes = new byte[availableLength]; System.out.println("avant le setCaontentLength"); response.setContentLength(availableLength); in.read(totalBytes); tempfile.write(totalBytes); System.out.println("Après le 1er write"); tempfile.flush(); tempfile.close(); in.close(); in = new FileInputStream(lstrDownloadPath + "/" + fileName); availableLength = in.available(); totalBytes = new byte[availableLength]; response.setContentLength(availableLength); in.read(totalBytes); out.write(totalBytes); out.flush(); out.close(); in.close(); File f = new File(lstrDownloadPath + "/" + fileName); f.delete(); } catch (FileNotFoundException e) { throw new HermesException(e.getMessage()); } catch (IOException e) { throw new HermesException(e.getMessage()); } } protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("DOPOST Download"); try { DemandeHelper demH = (DemandeHelper) request.getSession().getAttribute("demH"); ServletContext context = this.getServletConfig().getServletContext(); Tools.debugRequestParameters(request); String ext = request.getParameter("FILE"); String plusieurs = request.getParameter("plusieurs"); ServletOutputStream out = response.getOutputStream(); if (plusieurs == null) { String fileName = Tools.formatFileName( demH.getCcpCasTO().getCcpCasId() + "-" + demH.getReunionCcpHelper().getCcpTO().getCcpLib() + "-" + demH.getCcpCasTO().getCcpCasDateDemande()); if (ext.equals("pdf")) { fileName += ".pdf"; } else { fileName += ".xml"; } String lstrMimeType = context.getMimeType(fileName); String path = ListLibrary.getDataPath().concat(fileName); setFile(out,response, fileName, path, lstrMimeType); } else { System.out.println("Dans le else"); Delegate lobjDelegate = (Delegate) request.getSession().getAttribute("Delegate"); String[] lstrTab = request.getParameterValues("cases"); String[] lstrTabPath = new String[lstrTab.length]; String[] lstrTabFileName = new String[lstrTab.length]; CcpCasTO lobjCcpCasTO2; String path = ListLibrary.getDataPath(); String fileName = ""; for (int i = 0; i < lstrTab.length; i++) { System.out.println("Fichiers : " + lstrTab[i]); lobjCcpCasTO2 = lobjDelegate.findCcpCasTo(Integer.parseInt(lstrTab[i])); demH = lobjDelegate.getDemandeHelper(lobjCcpCasTO2); fileName = Tools.formatFileName( demH.getCcpCasTO().getCcpCasId() + "-" + demH .getReunionCcpHelper() .getCcpTO() .getCcpLib() + "-" + demH.getCcpCasTO().getCcpCasDateDemande()); if (ext.equals("pdf")) { fileName += ".pdf"; } else { fileName += ".xml"; } lstrTabFileName[i] = fileName; lstrTabPath[i] = path + fileName; System.out.println("Nom Fichier : " + lstrTabFileName[i]); System.out.println("Path : " + path); } System.out.println("Après la boucle for"); String lstrMimeType = context.getMimeType("essais.zip"); Tools.zippe(path + "essais.zip", lstrTabPath, lstrTabFileName); path += fileName; System.out.println("Après le zip"); setFile(out,response, "essais.zip", path, lstrMimeType); System.out.println("Après le setFile"); } RequestDispatcher lobjReqDisp = request.getRequestDispatcher("/FrontController?action=445"); lobjReqDisp.forward(request, response); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Message : "+ex.getMessage()); throw new HermesException(ex.getMessage()); } } }
Au fait quelle méthode commit les reponse? Je suis novice en servlet... :?:
par exemple les methodes :
- sendError (HttpServletResponse)
- sendRedirect (HttpServletResponse)
- flush (ServletOutputStream)
....
Pour savoir si la response est commité tu peux utilisé cette methode :
http://java.sun.com/products/servlet...#isCommitted()
Les methodes getOutputStream et getWriter ne peuvent pas etre utilisées ensemble.
Afin de tester, remplace
par çaCode:ServletOutputStream out = response.getOutputStream();
et regarde si tu as une IllegalStateException ....Code:
1
2 ServletOutputStream out = null; response.getWriter();