I have certain images that are uploaded by users of my application, and should only be accessed by them. How can I display them as thumbnails or otherwise. I store the images in filesystem (outside webapp), and only store their path in the db.
One option that I found was to configure tomcat to expose that directory via following changes in server.xml.
<Context path="/images" docBase="/home/app/basedir" /> And then access images via
localhost:8080/context/images/b1sd2e09102.jpg etc. (I am using UUID to generate random image names so that they can not be guessed)
However, this is not an ideal solution for me as the url (if known by other users somehow) is still public. Currently I am able to download them via following in my spring controller:
@RequestMapping(value = "/download", method = RequestMethod.GET) public void download(Model model, @RequestParam("id") String key, HttpServletResponse response) throws Exception { //get item from db first based on key Item item = dbservice.get(key); InputStream is = new FileInputStream(item.getPath()); response.setHeader("Content-Disposition", "attachment; filename=" + item.getFileName()); IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); .... } Is there a way to render these images on the browser ?