JSP读取BLOB

<%	Connection con = null;
	 try {
      con = Conn.getConnection(); 

      con.setAutoCommit(false); 

      Statement st = con.createStatement(); 

   	  ResultSet rs = st.executeQuery("SELECT Attachment_name,Attachment_content FROM Attachment WHERE attachmentRow_GUID='1001'"); 

      if (rs.next()) { 
      	
      	//String fileName = rs.getString(1);
      	//System.out.println(fileName);
        Blob blob = rs.getBlob(2); 

        InputStream ins = blob.getBinaryStream(); 

        response.setContentType("application/octet-stream"); 

        response.addHeader("Content-Disposition", "attachment; filename="+StringI18.toGBK(rs.getString(1))); 

        OutputStream outStream = response.getOutputStream(); 
        byte[] bytes = new byte[1024]; 
        int len = 0; 
        while ((len=ins.read(bytes))!=-1) { 
            outStream.write(bytes,0,len);
        } 
        ins.close(); 
        outStream.close(); 
        outStream = null; 
        con.commit(); 
        con.close(); 
      } 
      }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {
      if (con!=null) try { con.close(); } catch (SQLException ex) {}
    }
%>

 

你可能感兴趣的:(jsp)