java.sql.SQLException:在ie8浏览器oracle 10g R2应用服务器中下载blob文件时关闭连接

java.sql.SQLException: Closed Connection when blob file downloaded in ie8 browser oracle 10g R2 application server

当在 struts 1.3 中单击 hyperlink 时,我正在尝试从 oracle 10g 数据库下载附件。这导致以下异常

java.sql.SQLException: Closed Connection at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:137) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:174) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:239) at oracle.sql.BLOB.getDBAccess(BLOB.java:875) at oracle.sql.BLOB.length(BLOB.java:142) at _jsp._CLA__DownLoadSelected._jspService(_CLA__DownLoadSelected.java:73) at com.orionserver[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].http.OrionHttpJspPage.service(OrionHttpJspPage.java:56) at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:356) at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:498) at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:402) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:835) at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:341) at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:816) at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.AJPRequestHandler.run(AJPRequestHandler.java:231) at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.AJPRequestHandler.run(AJPRequestHandler.java:136) at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:186) at java.lang.Thread.run(Thread.java:534)

1. hyper link 代码看起来像:

<a href="./jsp/DownLoadSelected.jsp?TrnID=3333"></a>

2。 dowloadSelected.jsp 看起来像:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ page contentType="text/html;charset=windows-1252" %>
<%@ page import = "java.util.*" %>
 <%@ page import ="java.sql.*" %>
   <%@ page import="java.sql.Blob.*" %>
    <%@ page import="org.apache.commons.fileupload.*"%>
      <%@ page import ="javax.naming.InitialContext" %>
        <%@ page import = "java.io.*"%>
          <%@ page import ="java.sql.PreparedStatement.*" %>
            <%@ page import="oracle.sql.BLOB"%>
              <%@ page import="java.sql.Blob"%>
                <%@ page import="org.iibf.claim.dto.*"%> 
                 <html>
                   <head>
                    <title>DownLoad File Form</title>
                      <form name="upload1" method=get ENCTYPE='multipart/form-data'>
                        <link href="../stylesheet/style.css" rel="stylesheet" type="text/css" />
                        <script>
                         alert("Download pdf");
                        </script>
                       </head>
                      <body>

                      <form name="RT_DownloadAttach">

<%
 String PurchaseId=request.getParameter("TrnID");

  System.out.println(PurchaseId+ "<- trnid ");

  LM_DownLoadAttachDto objRT_DownLoadAttachDTO =org.iibf.claim.util.CLA_Conveyance_recommenHelper.getSelectedDownload(Purchas     eId);

//old line
   Blob blob= objRT_DownLoadAttachDTO.getblob();
 System.out.println("with sql blob : "+blob.length());   

 long l =blob.length();
 oracle.sql.BLOB bbb = objRT_DownLoadAttachDTO.getblob();
  l =bbb.length();
byte[] ba =blob.getBytes(1,(int)bbb.length());  
//java.io.InputStream fis = rs.getBlob(2).getBinaryStream();
 String fileName = objRT_DownLoadAttachDTO.getName();
 OutputStream os = response.getOutputStream();
 response.setContentType("text/ascii");
 response.setHeader("Content-Disposition","attachment;filename=\""+fileName+"\"");
  response.setHeader("cache-control", "no-cache");
  os.write(ba);
os.close(); %>
     <div id="body11">
   </body>
  <html>

3。辅助 class 方法看起来像

  public static DownLoadAttachDto getSelectedDownload(String ReqID) throws SQLException {
 String FunctionName="getSelectedDownload"; 
    boolean update = false;
    String strSQL="";
    int updatecount=0;
    Collection collAllDownLoad = null;
    PreparedStatement prepStmt= null;
    Connection objConnection = null;
    ResultSet rs = null;
    Vector vecDownload = new Vector();
    LM_DownLoadAttachDto objRT_DownLoadAttachDTO=null;
    strSQL = " SELECT UD.* FROM  CT_UPLOADATTACHMENT_DTLS UD " +
             " WHERE UD.TRN_ID = ? ";           
    try {
      objConnection = CLA_DALHelper.getConnection();
      prepStmt = objConnection.prepareStatement(strSQL);  
      prepStmt.setString(1,ReqID);
      rs = prepStmt.executeQuery();
      while(rs.next()) {
        objRT_DownLoadAttachDTO = new LM_DownLoadAttachDto();

        objRT_DownLoadAttachDTO.setName(rs.getString("ATTACH_NAME"));

        BLOB b = (oracle.sql.BLOB)rs.getBlob("ATTACH_FILE");
        System.out.println("with in recom b helper blob : "+b.length());
        Blob bb =rs.getBlob("ATTACH_FILE");
        System.out.println("with recom bb blob : "+bb.length());
        long l = bb.length();            objRT_DownLoadAttachDTO.setblob((oracle.sql.BLOB)rs.getBlob("ATTACH_FILE"));
        }
    }
    catch(SQLException ex) {
      throw new SQLException("Unable to get File " + ex.getMessage() ); 
    }
    finally{
      if(prepStmt!=null)
       prepStmt.close();
     if(rs!=null)
       rs.close();
       objConnection.close();      
    } return objRT_DownLoadAttachDTO;  }

我已经使用 LIVE 数据库通过代码进行调试,但无法重现此异常。此异常仅看到其已部署 war 文件的实时应用程序。

感谢@RoySix,我能够解决这个问题。就像他在评论中所说的那样,我在关闭连接之前在 DTO 对象 Setter 中设置了 BLOB 字节。然后通过对 DTO 的 getter 方法调用从 Helper Class 获得该字节。

下面是JSP的样子

<%@ page contentType="text/html;charset=windows-1252" %>
<%@ page import = "java.util.*" %>
 <%@ page import ="java.sql.*" %>
  <%@ page import="java.sql.Blob.*" %>
  <%@ page import="org.apache.commons.fileupload.*"%>
  <%@ page import ="javax.naming.InitialContext" %>
    <%@ page import = "java.io.*"%>
      <%@ page import ="java.sql.PreparedStatement.*" %>
        <%@ page import="oracle.sql.BLOB"%>
          <%@ page import="java.sql.Blob"%>
            <%@ page import="org.iibf.claim.dto.*"%> 
             <html>
               <head>
                <title>DownLoad File Form</title>
                  <form name="upload1" method=get ENCTYPE='multipart/form-data'>
                    <link href="../stylesheet/style.css" rel="stylesheet" type="text/css" />
                    <script>
                     alert("Download pdf");
                    </script>
                   </head>
                  <body>

                  <form name="RT_DownloadAttach">

 <%
  String PurchaseId=request.getParameter("TrnID");
   System.out.println(PurchaseId+ "<- trnid ");

   LM_DownLoadAttachDto objRT_DownLoadAttachDTO      =org.iibf.claim.util.CLA_Conveyance_recommenHelper.getSelectedDownload(Purchas     eId);
byte[] ba =objRT_DownLoadAttachDTO.getAttachmentByte(); 
//java.io.InputStream fis = rs.getBlob(2).getBinaryStream();
 String fileName = objRT_DownLoadAttachDTO.getName();
 OutputStream os = response.getOutputStream();
 response.setContentType("text/ascii");
 response.setHeader("Content-Disposition","attachment;filename=\""+fileName+"\"");
  response.setHeader("cache-control", "no-cache");
  os.write(ba);
os.close(); %>
     <div id="body11">
   </body>
  <html>

helper class 方法看起来像

......................
 while(rs.next()) {
    objRT_DownLoadAttachDTO = new LM_DownLoadAttachDto();
    objRT_DownLoadAttachDTO.setName(rs.getString("ATTACH_NAME"));
    BLOB b = (oracle.sql.BLOB)rs.getBlob("ATTACH_FILE");        
    Blob bb =rs.getBlob("ATTACH_FILE");        
    objRT_DownLoadAttachDTO.setblob((oracle.sql.BLOB)  rs.getBlob("ATTACH_FILE"));          
    oracle.sql.BLOB bbb = (oracle.sql.BLOB)rs.getBlob("ATTACH_FILE");
    l =bbb.length();
    byte[] ba =blob.getBytes(1,(int)bbb.length());
 }
.................