I am working on an application where the information of users gets added and modified(updated).
In add module, admin enters user details and unique-id(abc001) gets generated at "add" button. and admin also saves the image/picture(name : abc001) of the user in server location (//some-location-ip address/D drive/images).
In "update" module, admin can modify the user details, but can not modify id.
I need some direction in couple of scenarios.
If an Admin "updates" a particular user, the image of that user which is present in the server should gets displayed on the page as soon as the admin hit the update button.
Image code in JSP :
<img height="100px;" width="100px;" src="........." alt="Candidate Image"></img> I have written a servlet, but don't know how to call different images corresponding to different users and display the image on the profile page.
user A profile will display user A image user B profile will display user B image and so on
Servlet code Snippet
public class UpDatePhoto extends HttpServlet { public UpDatePhoto () { super(); // TODO Auto-generated constructor stub } private static final long serialVersionUID = -8071854868821235857L; private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB. private String imagePath; *public void init() throws ServletException { this.imagePath = "D:\\photo_not_available_large.png"; }* protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String requestedImage = request.getPathInfo(); if (requestedImage == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. return; } File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8")); String contentType = getServletContext().getMimeType(image.getName()); if (contentType == null || !contentType.startsWith("image")) { response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. return; } response.reset(); response.setBufferSize(DEFAULT_BUFFER_SIZE); response.setContentType(contentType); response.setHeader("Content-Length", String.valueOf(image.length())); response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\""); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE); output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally { close(output); close(input); } } private static void close(Closeable resource) { if (resource != null) { try { resource.close(); } catch (IOException e) { e.printStackTrace(); } } } } The image is not http accessible but is only accessible as a file, the servlet would have to open the image file, read in the contents and place those in the response buffer" ....not sure if i am correct.
Can somebody guide me to the direction and help me out as how to fetch the image from the server directory location and display the correct image for a user.