0 Replies Latest reply on Nov 1, 2005 12:29 AM by ws tuzi

    getBinaryStream() method can't get image from MSSQLServer

    ws tuzi Newbie

      I'm using JBoss3.2.3 and MS SQLServer2000?

      I could not get the image type data from MSSQLServer by using getBinaryStream() method. There's no exception throwed,but the image could not be displayed on the servlet page.

      If I connect the SQLServer through JDBC directly instead of using the DataSource of JBoss3.2.3,I can get the image from MSSQLServer by using getBinaryStream() method. The servlet code was not modified.

      The same servlet code deployed on Tomcat,the image can be taken out too.

      Why?Is this a bug of JBoss JCA DataSource or connection pool?JBoss don't support getBinaryStream method to MSSQLServer?
      Wonder anybody can shed some lights.
      Any comment is greatly appreciated!
      Thank you very much in advance!

      The servlet(BlobServlet.java) code as follows:

      package oa.web;

      import java.io.BufferedOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.ObjectInputStream;
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.Statement;
      import java.sql.Blob;
      import javax.servlet.ServletException;
      import javax.servlet.ServletOutputStream;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      public class BlobServlet extends HttpServlet {
      /**
      * Process the HTTP doGet request.
      */
      public synchronized void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
      try {
      String name = (String) request.getParameter("name");
      String col = (String) request.getParameter("col");
      String id = (String) request.getParameter("id");
      Connection con = DBConnection.getConnection();
      Statement stmt = con.createStatement(
      ResultSet.TYPE_SCROLL_INSENSITIVE,
      ResultSet.CONCUR_UPDATABLE);
      String sql = "select " + col + " from " + name + " where id='"
      + id + "'";
      java.sql.ResultSet rs = stmt.executeQuery(sql);
      while (rs.next()) {
      InputStream in = rs.getBinaryStream(col);
      response.setContentType(getMime(con, id, name));
      response.setContentType("image/jpeg");
      ServletOutputStream out = response.getOutputStream();
      BufferedOutputStream bos = new BufferedOutputStream(out);
      int count = -1;
      int total = 0;

      byte[] blob = new byte[in.available()];
      in.read(blob, 0, in.available());


      bos.write(blob);
      bos.flush();
      in.close();
      bos.close();
      }
      stmt.close();
      con.close();
      } catch (Exception e) {
      //throw new ServletException(e.getMessage());
      e.printStackTrace();
      }
      }

      protected String getMime(Connection con, String id, String tabname)
      throws Exception {
      String mime = "text/html; charset=GBK";
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
      ResultSet.CONCUR_UPDATABLE);
      String sql = "select mime from oa_filemap where ext=(select ext from "
      + tabname + " where id='" + id + "' )";
      java.sql.ResultSet rs = stmt.executeQuery(sql);
      while (rs.next()) {
      mime = rs.getString("mime");
      }
      rs.close();
      stmt.close();
      return mime;
      }
      }