I am trying to output jpegs from a database to the browser, but I don't know what to return from the handleRequest.
I've set the contentType and filled up the response, but when I return null I get a NPE.
Is this the right method for serving binaries? If so what am I supposed to return.
Code:public class GetImageController implements Controller { private ClipCache clipcache ; /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog (getClass ()) ; public ModelAndView handleRequest (HttpServletRequest request, HttpServletResponse response) throws Exception { String clipIdStr = StringUtils.clean (request.getParameter ("cid")) ; if (clipIdStr == null) return null ; String zoneIdStr = StringUtils.clean (request.getParameter ("zid")) ; if (zoneIdStr == null) return null ; int zid = StringUtils.toInt ("zoneid", zoneIdStr) ; int cid = StringUtils.toInt ("clipid", clipIdStr) ; logger.info ("Requested clip is zoneid=" + zid + " clipid=" + cid) ; Clip clip = clipcache.get (zid, cid) ; ClipThumbnail ct = clip.getClipThumbnail() ; response.setContentType (ct.getMimeType ()) ; OutputStream os = response.getOutputStream () ; InputStream fin = ct.getContent () ; byte[] buf = new byte[4096] ; int count = 0 ; while (true) { int n = fin.read (buf) ; if (n == -1) { break ; } count = count + n ; os.write (buf, 0, n) ; } os.flush () ; os.close () ; fin.close () ; return null ; } }


Reply With Quote