`
adventure
  • 浏览: 89781 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

JSP读取BLOB

JSP 
阅读更多
<%	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) {}
    }
%>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics