java下载文件成功但报错的解决方法

SEVERE: Servlet.service() for servlet [jsp] in context with path [/exchange] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:662)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:194)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
    at org.apache.jsp.pt.res.showFj_jsp._jspService(showFj_jsp.java:176)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at cn.com.huadi.aos.common.filter.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:190)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

六月 22, 2017 5:36:20 下午 org.apache.catalina.core.StandardWrapperValve invoke
out.clear();
out=pageContext.pushBody();
    //检查文件是否存在
        //文件名
        String realName = "updateP.sh";
        //路径
        String filePath = "";
        filePath="/opt/apache-tomcat-8.5.13/webapps/jk/wj/jb/"+ realName;
        //filePath = "D:\\work\\apache-tomcat-7.0.54_100\\webapps\\jk\\wj\\jb\\" + realName;
        File file = new File(filePath);
        if (!file.exists()) {
            throw new IOException("您要找的文件不存在!");
        } else {
            //指定IE及其他浏览器时下载文件名的编码格式
            if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0
                    || request.getHeader("User-Agent").toUpperCase().indexOf("TRIDENT") > 0) {
                realName = URLEncoder.encode(realName, "UTF-8");
            } else {
                realName = new String(realName.getBytes("UTF-8"), "ISO8859-1");
            }
            response.setHeader("Content-Disposition", "attachment; filename=" + realName);

            //下载
            //注意下面2句话!!!!,不加不影响使用但会报错
            out.clear();
            out=pageContext.pushBody();
            ServletOutputStream os = response.getOutputStream();
            FileInputStream in = new FileInputStream(file);
            try {
                byte[] buffer = new byte[8192];
                int bytes = -1;
                while ((bytes = in.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytes);
                }
                os.flush();

            } catch (Exception e) {
                throw new IOException("文件下载失败 \n erorr in downloadFileFromSystem()!");
            } finally {
                if (os != null) {
                    os.close();
                }
                if (in != null) {
                    in.close();
                }
            }

        }

你可能感兴趣的:(java)