Someone PLEASE Help!
I have been having hassles when using a BufferedOutputStream to send multiple images
from an Oracle database to the client. The error that I am getting is in the While
loop that sends the image as a byte[] to the client. "java.net.SocketException:
Connection reset by peer: socket write error". What I think is happening is that the
first image is been sent to the browser and thereafter the connection is closed! Is
there a way to send multiple images out to the client in a single Servlet (Possibly
using a buffer). P.S The reason why I am not using <IMG> tags and sepecifying the
source to point to another JSP or Servlet is because I am doing some timing methods in
a single Servlet that writes out to a file.
Thanks for your help.
Dylan
I have included the code below:
import java.util.Stack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;
import oracle.ord.im.OrdImage;
import oracle.ord.im.OrdHttpUploadFile;
import oracle.ord.im.OrdHttpUploadFormData;
import oracle.ord.im.OrdHttpResponseHandler;
import java.util.*;
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.*;
import oracle.ord.im.*;
import java.util.Stack;
public class ImageServlet extends HttpServlet
{
private final static String JDBC_CONNECT_STRING
="jdbc:oracle:thin:@athena:1521:media";
private final static String JDBC_USER_NAME = "mmedia";
private final static String JDBC_PASSWORD = "mmedia";
private static Stack connStack = new Stack();
private static boolean driverLoaded = false;
public void init( ServletConfig config ) throws ServletException
{
super.init(config);
}
public void doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
response.setContentType("image/jpeg");
response.setBufferSize(1024 * 1000);
Connection conn = null;
long startTime = System.currentTimeMillis();
long endTime;
try
{
conn = getConnection();
PreparedStatement stmt =
conn.prepareStatement( "select IMAGE from smallphotos where id between 0
and 501" );
//stmt.setString( 1, request.getParameter( "id" ) );
OracleResultSet rset = (OracleResultSet)stmt.executeQuery();
OrdImage img;
ServletOutputStream o = response.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(o);
Blob temp;
byte[] buf = new byte[1024];
while ( rset.next() )
{
img = (OrdImage)rset.getCustomDatum(1, OrdImage.getFactory());
temp = img.getContent();
buf = temp.getBytes(1,(int)temp.length()+1);
out.write(buf);
}
response.flushBuffer();
out.close();
rset.close();
stmt.close();
}
catch ( SQLException e )
{
//
// Log what went wrong.
//
e.printStackTrace( System.out );
//
// Turn SQL exceptions into ServletExceptions.
//
throw new ServletException( e.toString() );
}
finally
{
//
// If we have a JDBC connection, then return it to the pool.
//
freeConnection( conn );
}
endTime = System.currentTimeMillis();
long finalTime = endTime - startTime;
try
{
FileWriter fw = new FileWriter ("zzzz.txt");
BufferedWriter br = new BufferedWriter (fw);
PrintWriter pw = new PrintWriter (br);
pw.println (finalTime);
pw.flush();
pw.close();
br.close();
fw.close();
}
catch (IOException io)
{
System.out.println ( "FILEWRITER EXCEPTION " +io.getMessage() );
}
catch (Exception e)
{
System.out.println (" FILEWRITER GENERIC EXCEPTION " +e.getMessage());
}
}
/*
* Process an HTTP Post request used to upload a new photo into the album.
*/
public void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
Connection conn = null;
//
// Use a try-block to ensure that JDBC connections are always returned
// to the pool.
//
try
{
//
// Get a JDBC connection from the pool
//
conn = getConnection();
//
// Instantiate an PhotoAlbumRequest object to process the request
//
PhotoAlbumRequest albumRequest =
new PhotoAlbumRequest( conn, request, response );
//
// Insert the photo into the album.
//
albumRequest.insertNewPhoto();
}
catch ( SQLException e )
{
//
// Log what went wrong.
//
e.printStackTrace( System.out );
//
// Turn SQL exceptions into ServletExceptions.
//
throw new ServletException( e.toString() );
}
finally
{
//
// If we have ba JDBC connection, then return it to the pool.
//
freeConnection( conn );
}
}
/*
* Get Servlet information
*/
public String getServletInfo()
{
return "interMedia Java Servlet Photo Album Demo";
}
/**
* The getConnection method implements a simple JDBC connection pool using
* a Java Stack to hold the set of available connections. If the stack is
* empty, then getConnection simply creates a new connection.
*/
private Connection getConnection()
throws SQLException
{
Connection conn = null;
//
// Synchonize on the Stack object. Load the JDBC driver if not yet
// done. If there's a free connection on the stack, then pop it off
// the stack and return it to the caller. Otherwise, create a new
// connection object.
//
synchronized( connStack )
{
if ( !driverLoaded )
{
DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver() );
driverLoaded = true;
}
if ( connStack.empty() )
{
conn = DriverManager.getConnection
( JDBC_CONNECT_STRING, JDBC_USER_NAME, JDBC_PASSWORD );
}
else
{
conn = (Connection)connStack.pop();
}
}
//
// Enable auto-commit by default.
//
conn.setAutoCommit( true );
return conn;
}
/**
* The freeConnection method simply returns a JDBC connection to the pool.
*/
private void freeConnection( Connection conn )
{
//
// Synchonize on the Stack object, then push the connection onto the
// stack.
//
if ( conn != null )
{
synchronized( connStack )
{
connStack.push( conn );
}
}
}
}