1

Im being mad with a problem that is hard to resolve for me without your help. I'm creating a Tomcat's project with servlet/jstl/jsp technology and I have a problem uploding photos.

For example: a user update his profile image. He click on the submit button of the multipart form in the html and then a servlet will process his request. My servlet use multipart for uploading files. The servlet will update the image profile in the database with the new name of the photo (updating the user object in the session too) and, at the end, it will return a page where the profile image should be up to date, but it will not be loaded. If I inspect the img element, the path is right because if I copy the img source in the address-bar it will show the right image.

I suppose the problem is the following: the multipart request will process the upload the file asynchronous, so when the servlet redirect to the profile.jsp page, it will have trouble loading an image that is being written on disk at this moment. I suppose this is the problem, but I'm not sure. Is there a way to wait for multipart to finish loading the photo, because if I refresh the profile.jsp page, the photo is painted clearly on the screen without problems.

This is the code in the doPost method of the Servlet (semplified without null control, ...)

MultipartRequest multi = new MultipartRequest(request, manager.completePath + "/web" + dirName, 10 * 1024 * 1024, "ISO-8859-1", new DefaultFileRenamePolicy());

Enumeration files = multi.getFileNames();
String name = null;
while (files.hasMoreElements()) {
    name = (String) files.nextElement();
}

RequestDispatcher rd;

String newAvPath = multi.getFilesystemName(name);

HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
user.updateImgProfile(newAvPath);

rd = request.getRequestDispatcher("/profile.jsp");  

rd.forward(request, response);

Excuse me in advance for my English because I'm Italian and thanks all :)

Luca Di Liello
  • 1,486
  • 2
  • 17
  • 34

0 Answers0