Привет Я пытаюсь загрузить файл с моей веб-службы, я получаю файл от службы, однако я получаю исключение на линии, где я получение сценария ответа сервлета и не знаю, что вызывает проблему.java.lang.IllegalStateException: getWriter() уже вызван для этого ответа
private void downloadFile(HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream servletStream;
try {
if (fileImpl == null) {
fileImpl = new FileHandlerImplService();
}
if (service == null) {
service = fileImpl.getFileHandlerImplPort(new MTOMFeature());
}
String fileName = request.getParameter("fileName");
byte[] data = service.downloadFile(fileName,
request.getSession().getAttribute("user").toString().trim());
File file = new File(fileName);
OutputStream outStream = new FileOutputStream(file);
BufferedOutputStream bufferStream = new BufferedOutputStream(outStream);
bufferStream.write(data);
bufferStream.close();
String mimeType = getServletContext().getMimeType(file.getAbsolutePath());
response.setContentType(mimeType != null ? mimeType : "application/octet-stream");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
InputStream inputStream = new FileInputStream(file);
servletStream = response.getOutputStream();
byte[] bufferData = new byte[1024*1024];
int read = 0;
while ((read = inputStream.read(bufferData)) != -1) {
servletStream.write(bufferData, 0, read);
}
servletStream.flush();
servletStream.close();
inputStream.close();
file.deleteOnExit();
response.sendRedirect("downloadFile.jsp");
} catch (Exception e) {
e.printStackTrace();
}
}
Вот исключение я получаю
severe: java.lang.IllegalStateException: getWriter() has already been called for this response
at org.apache.catalina.connector.Response.getOutputStream(Response.java:746)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:206)
at servlets.FileRequestHandler.downloadFile(FileRequestHandler.java:90)
at servlets.FileRequestHandler.processRequest(FileRequestHandler.java:58)
at servlets.FileRequestHandler.doPost(FileRequestHandler.java:200)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
Info: visiting unvisited references
Info: visiting unvisited references
Info: visiting unvisited references
Info: Loading application [JigarJoshiWebClient] at [/JigarJoshiWebClient]
Info: JigarJoshiWebClient was successfully deployed in 481 milliseconds.
спасибо! Я заработал. Я не заметил строку для печати в начале кода! –