FW: Read image from database

"Zilberstein Yuval" <[email protected]>
Newsgroups gmane.comp.jakarta.taglibs.user
Message-ID <57BD1F79DFC66F478C7E14D7C5D3219F01652884@blackhawk3.pelephone.co.il>
Thanks a lot to:
- Kris Schneider
- Don Albertson
- Vikram Reddy
Your help is very appreciated.

I constructed the code like this:
- on the jsp I write the following snippet that maps to a servlet:

<img src="getImagePart?idx=" + partIdx + " width="200" height="200">

- My servlet code uses ImageInfo (see
http://schmidt.devlib.org/image-info/).
  Heres a snippet of the servlet method:

public class GetImagePart extends HttpServlet implements
SingleThreadModel{
	public void doGet(HttpServletRequest req, HttpServletResponse
res)
	throws ServletException, IOException {
		processRequest(req,res);
	}	

	public void processRequest(HttpServletRequest req,
HttpServletResponse res)
			throws ServletException, IOException {

		synchronized (GetImagePart.class) {
			String partIdxParam = req.getParameter("idx");
			int partIdx = Integer.parseInt(partIdxParam);
			InputStream imagenbIS = null;
			try {
				imagenbIS =
blobObj.getCONTENT().getBinaryStream();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
			ImageInfo ii = new ImageInfo();
			 ii.setInput(imagenbIS);
			 imagenbIS.mark(9999999);
			 String mimeType = null;
			 boolean isImageStream = ii.check();
			 imagenbIS.reset();
			 if (!isImageStream) {
				 mimeType = "text/html";
			 }
			 else {
				 mimeType = ii.getMimeType();
			}
			res.setContentType(mimeType);
			if (imagenbIS == null) {
	
res.sendError(HttpServletResponse.SC_NOT_FOUND);
				return;
			}

		// Write the image stream to the response.
			ByteBuffer buffer =
ByteBuffer.allocateDirect(9999999);
			ReadableByteChannel readChannel =
Channels.newChannel(imagenbIS);
			WritableByteChannel writeChannel =
Channels.newChannel(res
					.getOutputStream());
			while (readChannel.read(buffer) != -1 &&
writeChannel.isOpen()) {
				buffer.flip();
				writeChannel.write(buffer);
				buffer.clear();
			}

			writeChannel.close();
			readChannel.close();
			imagenbIS.close();
		}
	}

	private static void printlist(String names[], String title,
HttpServletResponse res) throws IOException {
		 res.getOutputStream().println(title + "<br>");
		for (int i = 0, n = names.length; i < n; i++) {
			 res.getOutputStream().println("\t" + names[i] +
"<br>");
		}
	}

}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.