2.上传的文件即使下载成功,在tomcat的表面也看不到错误,但在tomcat的日志里会有这种记载2005-02-17 08:58:57 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:750)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:165)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)
at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:177)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:198)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:193)
at org.apache.jsp.dnowload$jsp._jspService(dnowload$jsp.java:116)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at com.wh.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:25)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:534)
请问怎样解决这样的问题呢?

解决方案 »

  1.   

    这是那个smartupload类,请大家帮忙看看,谢谢!!!
      

  2.   

    输出流在提示下载前已经传到response里了,所以在下载前response.reset()一下,应该就可以了。我原来遇到过类似问题!
      

  3.   

    具体怎么做呢?是在下载download.jsp页response.reset(),还是在smartupload类里?
    sorry,我刚学,不太懂,以前我只是做网页,涉及这些方面的少。
      

  4.   

    而且response.reset()是不是只能解决第二个问题,并不能解决*.txt不能下载的问题?
      

  5.   

    在下载前RESPONSE有输出了(OUTPUT),这应该是不允许的吧
      

  6.   

    当文件太小时,*.txt文件会打开,而不是下载
      

  7.   

    可以通过以下代码下载
     FileInputStream input=new FileInputStream(path);
        byte[] byteS=new byte[input.available()];
        OutputStream ops = response.getOutputStream();
        input.read(byteS);
      

  8.   

    这是download.jsp页
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <head>
    <title>download</title>
    <%@ page import="com.jspsmart.upload.*"%>
    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
    </head>
    <% // Initialization
    mySmartUpload.initialize(pageContext);

    // Download file
    //mySmartUpload.downloadFile("/upload/你好吗.zip");

    // With a physical path
    // mySmartUpload.downloadFile("c:\\temp\\sample.zip")

    // With options
    String slavename=request.getParameter("file").trim();
    // With options
    String BSCaption=request.getParameter("file0").trim();
    String inbidID=new String(BSCaption);


     mySmartUpload.downloadFile("/qsfile/"+inbidID,"application/x-msdownload",slavename);
    %>
      

  9.   

    下载时用到的方法,是写到类里面的,下面就是那个java类
     public void downloadField(ResultSet rs, String columnName, String contentType, String destFileName)
            throws SQLException, IOException, ServletException
        {
            if(rs == null)
                throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
            if(columnName == null)
                throw new IllegalArgumentException("The columnName cannot be null (1050).");
            if(columnName.length() == 0)
                throw new IllegalArgumentException("The columnName cannot be empty (1055).");
            byte b[] = rs.getBytes(columnName);
            if(contentType == null)
                m_response.setContentType("application/x-msdownload");
            else
            if(contentType.length() == 0)
                m_response.setContentType("application/x-msdownload");
            else
                m_response.setContentType(contentType);
            m_response.setContentLength(b.length);
            if(destFileName == null)
                m_response.setHeader("Content-Disposition", "attachment;");
            else
            if(destFileName.length() == 0)
                m_response.setHeader("Content-Disposition", "attachment;");
            else
                m_response.setHeader("Content-Disposition", "attachment; filename=".concat(String.valueOf(destFileName)));
            m_response.getOutputStream().write(b, 0, b.length);
        }
      

  10.   

    这个就是全部的
    public void downloadFile(String sourceFilePathName)
            throws SmartUploadException, IOException, ServletException
        {
            downloadFile(sourceFilePathName, null, null);
        }    public void downloadFile(String sourceFilePathName, String contentType)
            throws SmartUploadException, IOException, ServletException
        {
            downloadFile(sourceFilePathName, contentType, null);
        }    public void downloadFile(String sourceFilePathName, String contentType, String destFileName)
            throws SmartUploadException, IOException, ServletException
        {
            downloadFile(sourceFilePathName, contentType, destFileName, 65000);
        }    public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize)
            throws SmartUploadException, IOException, ServletException
        {
            if(sourceFilePathName == null)
                throw new IllegalArgumentException(String.valueOf((new StringBuffer("File '")).append(sourceFilePathName).append("' not found (1040).")));
            if(sourceFilePathName.equals(""))
                throw new IllegalArgumentException(String.valueOf((new StringBuffer("File '")).append(sourceFilePathName).append("' not found (1040).")));
            if(!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
                throw new SecurityException("Physical path is denied (1035).");
            if(isVirtual(sourceFilePathName))
                sourceFilePathName = m_application.getRealPath(sourceFilePathName);
            File file = new File(sourceFilePathName);
            FileInputStream fileIn = new FileInputStream(file);
            long fileLen = file.length();
            int readBytes = 0;
            int totalRead = 0;
            byte b[] = new byte[blockSize];
            if(contentType == null)
                m_response.setContentType("application/x-msdownload");
            else
            if(contentType.length() == 0)
                m_response.setContentType("application/x-msdownload");
            else
                m_response.setContentType(contentType);
            m_response.setContentLength((int)fileLen);
            m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
            if(destFileName == null)
                m_response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(getFileName(sourceFilePathName))));
            else
            if(destFileName.length() == 0)
                m_response.setHeader("Content-Disposition", m_contentDisposition);
            else
                m_response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(destFileName)));
            while((long)totalRead < fileLen) 
            {
                readBytes = fileIn.read(b, 0, blockSize);
                totalRead += readBytes;
                m_response.getOutputStream().write(b, 0, readBytes);
            }
            fileIn.close();
        }    public void downloadField(ResultSet rs, String columnName, String contentType, String destFileName)
            throws SQLException, IOException, ServletException
        {
            if(rs == null)
                throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
            if(columnName == null)
                throw new IllegalArgumentException("The columnName cannot be null (1050).");
            if(columnName.length() == 0)
                throw new IllegalArgumentException("The columnName cannot be empty (1055).");
            byte b[] = rs.getBytes(columnName);
            if(contentType == null)
                m_response.setContentType("application/x-msdownload");
            else
            if(contentType.length() == 0)
                m_response.setContentType("application/x-msdownload");
            else
                m_response.setContentType(contentType);
            m_response.setContentLength(b.length);
            if(destFileName == null)
                m_response.setHeader("Content-Disposition", "attachment;");
            else
            if(destFileName.length() == 0)
                m_response.setHeader("Content-Disposition", "attachment;");
            else
                m_response.setHeader("Content-Disposition", "attachment; filename=".concat(String.valueOf(destFileName)));
            m_response.getOutputStream().write(b, 0, b.length);
        }
      

  11.   

    这是我写的下载文件代码,你参考吧
    <%@ page contentType="text/html;charset=gb2312" errorPage="../../error.jsp"%>
    <%@page import="java.io.*"%>
    <%
      String filename = request.getParameter("filename");
      String sLocalFileName= request.getParameter("lfilename");
    //读到流中
      InputStream inStream=new FileInputStream(filename);
     //设置输出的格式
      response.reset();
      response.setContentType("bin");
      //response.setContentType("application/x-msdownload"); //
      response.addHeader("Content-Disposition","attachment; filename=\"" + sLocalFileName + "\"");
     //循环取出流中的数据
      byte[] b = new byte[100];
      int len;
      while((len=inStream.read(b)) >0) {
          response.getOutputStream().write(b,0,len);
      }
      response.getOutputStream().flush();
      inStream.close();
    %>
      

  12.   

    fbysss(独孤求败):你写的这个不用引用类文件的吗?