JSP: Call servlet Display Image
Since I work needs, I started learning Java and JSP. I will write more articles about JSP for this time to strengthen my understanding of JSP.
This article mainly introduces how to call servlet to display pictures, actually let you know how to output binary data to the client.
Under this 1.HTM used to call servlet
INT i = hfile.available (); // Get file size
Byte data [] = new byte [i];
Hfile.read (data); // read data
Hfile.close ();
Res.SetContentType ("image / *"); // Set the file type returned
OutputStream Toclient = Res. GetOutputStream (); // Get objects to the client output binary data
TOCLIENT.WRITE (data); // Output Data
TOCLIENT.CLOSE ();
}
Catch (IOException E) // Error Process
{
PrintWriter TOCLIENT = Res. Getwriter (); // Get objects to the client output text
Res.SetContentType ("text / html; charSet = GB2312");
TOCLIENT.WRITE ("Unable to open the picture!");
TOCLIENT.CLOSE ();
}
}