tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Zilberstein Yuval" <YUVA...@pelephone.co.il>
Subject FW: Read image from database
Date Mon, 16 Apr 2007 07:38:09 GMT
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>");
		}
	}

}



---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Mime
View raw message