简体   繁体   中英

java.lang.IllegalStateException: getWriter() has already been called for this response

Hi I am trying to download a file from my web service, I am getting the file from service however I am getting an exception at line where I am getting servlet response writer, and do not know whats causing the problem.

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();
        }
    }

Here is the exception I am getting

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.

You should really move data logic off Servlet probably to DataAccessLayer.

http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletResponse.html

The reason i think you are getting IllegalStateException is because you are calling response.sendRedirect() after committing the output ie after doing flush().

You can remove the response.sendRedirect() line and try running the application. It should work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM