0

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.

1 Answer 1

1

I have a hard time in understanding the concrete problem, but I believe that your root problem is that you don't know how to set the imagePath accordingly? It has namely a wrong value.The code shows that it should be set to the root folder where all images are been placed. In the underlying operating system platform, you need to map //some-location-ip address/D drive/images as a network drive in Windows explorer, e.g. Z: and then use that in your imagePath instead.

this.imagePath = "Z:"; 

It also expects the image file name as request pathinfo. So, assuming that your servlet is mapped on an URL pattern of /images/*, then your <img src> should look basically like this

<img src="images/filename.png" /> 

You could also fill it dynamically with EL. E.g. with the unique username of the logged-in user:

<img src="images/${user.name}.png" /> 

As to using the "D:\\photo_not_available_large.png" replacement image, you could set that when File#exists() returns false.

Sign up to request clarification or add additional context in comments.

1 Comment

Apologies If the question is vague and many thanks for suggestions :), will give it a try

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.