I am trying to send binary data from servlet to applet

My servlet read an image from the database. The type of that image that I got from the database  is InputStream
So I tried to write to the OutputStream in servlet in order to send the data back to the applet by using the following:

response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
byte[] buf = new byte[8000];
int n = 0;
while ((n = in.read(buf)) > 0)
{
     out.write(buf, 0, n);
}
out.flush();
 

Then in applet, i used the following to read the returned data

In the applet:
-------------
Image img;   // A member variable of the applet.
InputStream in = url.openStream();   //<----my program died here
byte[] buf = new byte[8000];
ByteArrayOutputStream bout = new ByteArrayOutputStream(30000);
int n = 0;
while ((n = in.read(buf)) > 0)
{
     bout.write(buf, 0, n);
}
bout.close();
in.close();

byte[] imageBytes = bout.toByteArray();  //by using the byte array, I should be able to create the image again

--------------------------------------------------------

However, the program was dead at InputStream in = url.openStream();
saying  java.io.FileNotFoundException: http//localhost:8000/servlet/testDBServlet

where testDBServlet is the servlet that I wrote.

Anyone knows why there is such an error and how to solve it?
 

Thanks a million.
 
 
 
 
 

Reply via email to